Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization.ProxyObjectReference.OnDeserialization
ProxyObjectReference.cs
Source:ProxyObjectReference.cs
...92 proxyGenerationOptions =93 (ProxyGenerationOptions)info.GetValue("__proxyGenerationOptions", typeof(ProxyGenerationOptions));94 proxy = RecreateProxy();95 // We'll try to deserialize as much of the proxy state as possible here. This is just best effort; due to deserialization dependency reasons,96 // we need to repeat this in OnDeserialization to guarantee correct state deserialization.97 DeserializeProxyState();98 }99 private Type DeserializeTypeFromString(string key)100 {101 return Type.GetType(info.GetString(key), true, false);102 }103#if FEATURE_SECURITY_PERMISSIONS && DOTNET40104 [SecurityCritical]105#endif106 protected virtual object RecreateProxy()107 {108 var generatorType = GetValue<string>("__proxyTypeId");109 if (generatorType.Equals(ProxyTypeConstants.Class))110 {111 isInterfaceProxy = false;112 return RecreateClassProxy();113 }114 if (generatorType.Equals(ProxyTypeConstants.ClassWithTarget))115 {116 isInterfaceProxy = false;117 return RecreateClassProxyWithTarget();118 }119 isInterfaceProxy = true;120 return RecreateInterfaceProxy(generatorType);121 }122#if FEATURE_SECURITY_PERMISSIONS && DOTNET40123 [SecurityCritical]124#endif125 private object RecreateClassProxyWithTarget()126 {127 var generator = new ClassProxyWithTargetGenerator(scope, baseType, interfaces, proxyGenerationOptions);128 var proxyType = generator.GetGeneratedType();129 return InstantiateClassProxy(proxyType);130 }131#if FEATURE_SECURITY_PERMISSIONS && DOTNET40132 [SecurityCritical]133#endif134 public object RecreateInterfaceProxy(string generatorType)135 {136 var @interface = DeserializeTypeFromString("__theInterface");137 var targetType = DeserializeTypeFromString("__targetFieldType");138 InterfaceProxyWithTargetGenerator generator;139 if (generatorType == ProxyTypeConstants.InterfaceWithTarget)140 {141 generator = new InterfaceProxyWithTargetGenerator(scope, @interface);142 }143 else if (generatorType == ProxyTypeConstants.InterfaceWithoutTarget)144 {145 generator = new InterfaceProxyWithoutTargetGenerator(scope, @interface);146 }147 else if (generatorType == ProxyTypeConstants.InterfaceWithTargetInterface)148 {149 generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, @interface);150 }151 else152 {153 throw new InvalidOperationException(154 string.Format(155 "Got value {0} for the interface generator type, which is not known for the purpose of serialization.",156 generatorType));157 }158 var proxyType = generator.GenerateCode(targetType, interfaces, proxyGenerationOptions);159 return FormatterServices.GetSafeUninitializedObject(proxyType);160 }161#if FEATURE_SECURITY_PERMISSIONS && DOTNET40162 [SecurityCritical]163#endif164 public object RecreateClassProxy()165 {166 var generator = new ClassProxyGenerator(scope, baseType);167 var proxyType = generator.GenerateCode(interfaces, proxyGenerationOptions);168 return InstantiateClassProxy(proxyType);169 }170#if FEATURE_SECURITY_PERMISSIONS && DOTNET40171 [SecurityCritical]172#endif173 private object InstantiateClassProxy(Type proxy_type)174 {175 delegateToBase = GetValue<bool>("__delegateToBase");176 if (delegateToBase)177 {178 return Activator.CreateInstance(proxy_type, new object[] { info, context });179 }180 else181 {182 return FormatterServices.GetSafeUninitializedObject(proxy_type);183 }184 }185 protected void InvokeCallback(object target)186 {187 if (target is IDeserializationCallback)188 {189 (target as IDeserializationCallback).OnDeserialization(this);190 }191 }192#if FEATURE_SECURITY_PERMISSIONS && DOTNET40193 [SecurityCritical]194#endif195 public object GetRealObject(StreamingContext context)196 {197 return proxy;198 }199#if FEATURE_SECURITY_PERMISSIONS && DOTNET40200 [SecurityCritical]201#endif202 public void GetObjectData(SerializationInfo info, StreamingContext context)203 {204 // There is no need to implement this method as 205 // this class would never be serialized.206 }207#if FEATURE_SECURITY_PERMISSIONS && DOTNET40208 [SecuritySafeCritical]209#endif210 public void OnDeserialization(object sender)211 {212 var interceptors = GetValue<IInterceptor[]>("__interceptors");213 SetInterceptors(interceptors);214 DeserializeProxyMembers();215 // Get the proxy state again, to get all those members we couldn't get in the constructor due to deserialization ordering.216 DeserializeProxyState();217 InvokeCallback(proxy);218 }219#if FEATURE_SECURITY_PERMISSIONS && DOTNET40220 [SecurityCritical]221#endif222 private void DeserializeProxyMembers()223 {224 var proxyType = proxy.GetType();...
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7{8 {9 static void Main(string[] args)10 {11 ProxyObjectReference proxyObjectReference = new ProxyObjectReference();12 proxyObjectReference.OnDeserialization(null);13 }14 }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;22{23 {24 static void Main(string[] args)25 {26 ProxyObjectReference proxyObjectReference = new ProxyObjectReference();27 proxyObjectReference.OnDeserialization(null);28 }29 }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;37{38 {39 static void Main(string[] args)40 {41 ProxyObjectReference proxyObjectReference = new ProxyObjectReference();42 proxyObjectReference.OnDeserialization(null);43 }44 }45}46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;52{53 {54 static void Main(string[] args)55 {56 ProxyObjectReference proxyObjectReference = new ProxyObjectReference();57 proxyObjectReference.OnDeserialization(null);58 }59 }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;67{68 {
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7using Telerik.JustMock.Core.Context;8using Telerik.JustMock.Core.MatcherTree;9{10 {11 static void Main(string[] args)12 {13 var mock = Mock.Create<IClass>();14 Mock.Arrange(() => mock.Method()).Returns(1);15 var proxy = Mock.Get(mock);16 var matcher = new MatcherTreeBuilder().Build(proxy);17 var proxyObjectReference = new ProxyObjectReference(proxy, matcher);18 var deserializedProxy = proxyObjectReference.OnDeserialization(null);19 var deserializedMock = Mock.Get(deserializedProxy);20 Mock.Assert(() => deserializedMock.Method(), Occurs.Once());21 }22 }23 {24 int Method();25 }26}
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.IO;4using System.Runtime.Serialization.Formatters.Binary;5using Telerik.JustMock.Core;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7{8 {9 static void Main(string[] args)10 {11 var proxy = Mock.Create<List<int>>();12 var formatter = new BinaryFormatter();13 using (var stream = new MemoryStream())14 {15 formatter.Serialize(stream, proxy);16 stream.Position = 0;17 var deserializedProxy = (List<int>)formatter.Deserialize(stream);18 deserializedProxy.Add(1);19 Mock.Assert(() => deserializedProxy.Add(1), Occurs.Once());20 }21 }22 }23}
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7using Telerik.JustMock.Helpers;8using System.Runtime.Serialization;9using System.IO;10using System.Runtime.Serialization.Formatters.Binary;11using System.Reflection;12{13 {14 static void Main(string[] args)15 {16 var mock = Mock.Create<TestClass>();17 var proxy = new ProxyObjectReference(mock);18 BinaryFormatter formatter = new BinaryFormatter();19 MemoryStream stream = new MemoryStream();20 formatter.Serialize(stream, proxy);21 stream.Seek(0, SeekOrigin.Begin);22 var proxy2 = (ProxyObjectReference)formatter.Deserialize(stream);23 proxy2.OnDeserialization(null);24 mock.DoSomething();25 Mock.Assert(() => mock.DoSomething(), Occurs.Once());26 }27 }28 {29 public virtual void DoSomething()30 {31 Console.WriteLine("DoSomething");32 }33 }34}
OnDeserialization
Using AI Code Generation
1using System;2using System.IO;3using System.Runtime.Serialization;4using System.Runtime.Serialization.Formatters.Binary;5{6 {7 static void Main(string[] args)8 {9 var formatter = new BinaryFormatter();10 var stream = new MemoryStream();11 formatter.Serialize(stream, new SampleClass());12 stream.Position = 0;13 formatter.Deserialize(stream);14 }15 }16 {17 public SampleClass()18 {19 Console.WriteLine("Constructor");20 }21 public SampleClass(SerializationInfo info, StreamingContext context)22 {23 Console.WriteLine("Deserialization Constructor");24 }25 public void GetObjectData(SerializationInfo info, StreamingContext context)26 {27 Console.WriteLine("GetObjectData");28 }29 public void OnDeserialized(StreamingContext context)30 {31 Console.WriteLine("OnDeserialized");32 }33 public void OnDeserializing(StreamingContext context)34 {35 Console.WriteLine("OnDeserializing");36 }37 public void OnSerialized(StreamingContext context)38 {39 Console.WriteLine("OnSerialized");40 }41 public void OnSerializing(StreamingContext context)42 {43 Console.WriteLine("OnSerializing");44 }45 }46}
OnDeserialization
Using AI Code Generation
1using System;2using System.IO;3using System.Runtime.Serialization.Formatters.Binary;4using Telerik.JustMock.Core;5using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;6{7 {8 public virtual string MyMethod(string str)9 {10 return str;11 }12 }13 {14 static void Main(string[] args)15 {16 var myClass = Mock.Create<MyClass>();17 var stream = new MemoryStream();18 var formatter = new BinaryFormatter();19 formatter.Serialize(stream, myClass);20 stream.Seek(0, SeekOrigin.Begin);21 var proxyObjectReference = formatter.Deserialize(stream);22 var proxyObject = proxyObjectReference.OnDeserialization(null);23 Console.WriteLine("Deserialized proxy object is of type: {0}", proxyObject.GetType().FullName);24 Console.WriteLine("Deserialized proxy object is instance of: {0}", proxyObject is MyClass);25 Console.WriteLine("Deserialized proxy object is instance of: {0}", proxyObject is IProxy);26 }27 }28}
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Runtime.Serialization;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7using System.IO;8using System.Runtime.Serialization.Formatters.Binary;9using System.Reflection;10using Telerik.JustMock.Core;11{12 {13 static void Main(string[] args)14 {15 var mock = Mock.Create<ISimpleInterface>();16 Mock.Arrange(() => mock.SimpleMethod()).Returns("Hello");17 var formatter = new BinaryFormatter();18 var stream = new MemoryStream();19 formatter.Serialize(stream, mock);20 stream.Position = 0;21 var deserializedMock = formatter.Deserialize(stream) as ISimpleInterface;22 Console.WriteLine(deserializedMock.SimpleMethod());23 Console.ReadLine();24 }25 }26 {27 string SimpleMethod();28 }29}
OnDeserialization
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;7using Telerik.JustMock.Core.Model;8using Telerik.JustMock.Helpers;9{10 {11 public void Test()12 {13 var mock = Mock.Create<TestClass>();14 var proxy = Mock.Get(mock);15 var proxyObjectReference = proxy.GetType().GetField("proxyObjectReference", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(proxy) as ProxyObjectReference;16 proxyObjectReference.GetType().GetMethod("OnDeserialization", new Type[] { typeof(object) }).Invoke(proxyObjectReference, new object[] { null });17 }18 }19 {20 }21}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!