Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions.GetObjectData
ClassProxyInstanceContributor.cs
Source:ClassProxyInstanceContributor.cs
...26 using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;27 internal class ClassProxyInstanceContributor : ProxyInstanceContributor28 {29#if FEATURE_SERIALIZATION30 private readonly bool delegateToBaseGetObjectData;31 private readonly bool implementISerializable;32 private ConstructorInfo serializationConstructor;33 private readonly IList<FieldReference> serializedFields = new List<FieldReference>();34#endif35 public ClassProxyInstanceContributor(Type targetType, IList<MethodInfo> methodsToSkip, Type[] interfaces,36 string typeId)37 : base(targetType, interfaces, typeId)38 {39#if FEATURE_SERIALIZATION40 if (targetType.IsSerializable)41 {42 implementISerializable = true;43 delegateToBaseGetObjectData = VerifyIfBaseImplementsGetObjectData(targetType, methodsToSkip);44 }45#endif46 }47 protected override Reference GetTargetReference(ClassEmitter emitter)48 {49 return SelfReference.Self;50 }51 public override void Generate(ClassEmitter @class, ProxyGenerationOptions options)52 {53 var interceptors = @class.GetField("__interceptors");54#if FEATURE_SERIALIZATION55 if (implementISerializable)56 {57 ImplementGetObjectData(@class);58 Constructor(@class);59 }60#endif61 ImplementProxyTargetAccessor(@class, interceptors);62 foreach (var attribute in targetType.GetTypeInfo().GetNonInheritableAttributes())63 {64 @class.DefineCustomAttribute(attribute.Builder);65 }66 }67#if FEATURE_SERIALIZATION68 protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,69 FieldReference field)70 {71 serializedFields.Add(field);72 base.AddAddValueInvocation(serializationInfo, getObjectData, field);73 }74 protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo,75 ArgumentReference streamingContext, ClassEmitter emitter)76 {77 codebuilder.AddStatement(new ExpressionStatement(78 new MethodInvocationExpression(79 serializationInfo,80 SerializationInfoMethods.AddValue_Bool,81 new ConstReference("__delegateToBase").ToExpression(),82 new ConstReference(delegateToBaseGetObjectData).83 ToExpression())));84 if (delegateToBaseGetObjectData == false)85 {86 EmitCustomGetObjectData(codebuilder, serializationInfo);87 return;88 }89 EmitCallToBaseGetObjectData(codebuilder, serializationInfo, streamingContext);90 }91 private void EmitCustomGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo)92 {93 var members = codebuilder.DeclareLocal(typeof(MemberInfo[]));94 var data = codebuilder.DeclareLocal(typeof(object[]));95 var getSerializableMembers = new MethodInvocationExpression(96 null,97 FormatterServicesMethods.GetSerializableMembers,98 new TypeTokenExpression(targetType));99 codebuilder.AddStatement(new AssignStatement(members, getSerializableMembers));100 // Sort to keep order on both serialize and deserialize side the same, c.f DYNPROXY-ISSUE-127101 var callSort = new MethodInvocationExpression(102 null,103 TypeUtilMethods.Sort,104 members.ToExpression());105 codebuilder.AddStatement(new AssignStatement(members, callSort));106 var getObjectData = new MethodInvocationExpression(107 null,108 FormatterServicesMethods.GetObjectData,109 SelfReference.Self.ToExpression(),110 members.ToExpression());111 codebuilder.AddStatement(new AssignStatement(data, getObjectData));112 var addValue = new MethodInvocationExpression(113 serializationInfo,114 SerializationInfoMethods.AddValue_Object,115 new ConstReference("__data").ToExpression(),116 data.ToExpression());117 codebuilder.AddStatement(new ExpressionStatement(addValue));118 }119 private void EmitCallToBaseGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo,120 ArgumentReference streamingContext)121 {122 var baseGetObjectData = targetType.GetMethod("GetObjectData",123 new[] { typeof(SerializationInfo), typeof(StreamingContext) });124 codebuilder.AddStatement(new ExpressionStatement(125 new MethodInvocationExpression(baseGetObjectData,126 serializationInfo.ToExpression(),127 streamingContext.ToExpression())));128 }129 private void Constructor(ClassEmitter emitter)130 {131 if (!delegateToBaseGetObjectData)132 {133 return;134 }135 GenerateSerializationConstructor(emitter);136 }137 private void GenerateSerializationConstructor(ClassEmitter emitter)138 {139 var serializationInfo = new ArgumentReference(typeof(SerializationInfo));140 var streamingContext = new ArgumentReference(typeof(StreamingContext));141 var ctor = emitter.CreateConstructor(serializationInfo, streamingContext);142 ctor.CodeBuilder.AddStatement(143 new ConstructorInvocationStatement(serializationConstructor,144 serializationInfo.ToExpression(),145 streamingContext.ToExpression()));146 foreach (var field in serializedFields)147 {148 var getValue = new MethodInvocationExpression(serializationInfo,149 SerializationInfoMethods.GetValue,150 new ConstReference(field.Reference.Name).ToExpression(),151 new TypeTokenExpression(field.Reference.FieldType));152 ctor.CodeBuilder.AddStatement(new AssignStatement(153 field,154 new ConvertExpression(field.Reference.FieldType,155 typeof(object),156 getValue)));157 }158 ctor.CodeBuilder.AddStatement(new ReturnStatement());159 }160 private bool VerifyIfBaseImplementsGetObjectData(Type baseType, IList<MethodInfo> methodsToSkip)161 {162 if (!typeof(ISerializable).IsAssignableFrom(baseType))163 {164 return false;165 }166 if (IsDelegate(baseType))167 {168 //working around bug in CLR which returns true for "does this type implement ISerializable" for delegates169 return false;170 }171 // If base type implements ISerializable, we have to make sure172 // the GetObjectData is marked as virtual173 var getObjectDataMethod = baseType.GetInterfaceMap(typeof(ISerializable)).TargetMethods[0];174 if (getObjectDataMethod.IsPrivate) //explicit interface implementation175 {176 return false;177 }178 if (!getObjectDataMethod.IsVirtual || getObjectDataMethod.IsFinal)179 {180 var message = String.Format("The type {0} implements ISerializable, but GetObjectData is not marked as virtual. " +181 "Dynamic Proxy needs types implementing ISerializable to mark GetObjectData as virtual " +182 "to ensure correct serialization process.",183 baseType.FullName);184 throw new ArgumentException(message);185 }186 methodsToSkip.Add(getObjectDataMethod);187 serializationConstructor = baseType.GetConstructor(188 BindingFlags.Instance | BindingFlags.Public |189 BindingFlags.NonPublic,190 null,191 new[] { typeof(SerializationInfo), typeof(StreamingContext) },192 null);193 if (serializationConstructor == null)194 {195 var message = String.Format("The type {0} implements ISerializable, " +...
ProxyInstanceContributor.cs
Source:ProxyInstanceContributor.cs
...46 public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)47 {48 var interceptors = @class.GetField("__interceptors");49#if FEATURE_SERIALIZATION50 ImplementGetObjectData(@class);51#endif52 ImplementProxyTargetAccessor(@class, interceptors);53 foreach (var attribute in targetType.GetTypeInfo().GetNonInheritableAttributes())54 {55 @class.DefineCustomAttribute(attribute.Builder);56 }57 }58 protected void ImplementProxyTargetAccessor(ClassEmitter emitter, FieldReference interceptorsField)59 {60 var dynProxyGetTarget = emitter.CreateMethod("DynProxyGetTarget", typeof(object));61 dynProxyGetTarget.CodeBuilder.AddStatement(62 new ReturnStatement(new ConvertExpression(typeof(object), targetType, GetTargetReferenceExpression(emitter))));63 var dynProxySetTarget = emitter.CreateMethod("DynProxySetTarget", typeof(void), typeof(object));64 // we can only change the target of the interface proxy65 var targetField = GetTargetReference(emitter) as FieldReference;66 if (targetField != null)67 {68 dynProxySetTarget.CodeBuilder.AddStatement(69 new AssignStatement(targetField,70 new ConvertExpression(targetField.Fieldbuilder.FieldType, dynProxySetTarget.Arguments[0].ToExpression())));71 }72 else73 {74 dynProxySetTarget.CodeBuilder.AddStatement(75 new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy."));76 }77 dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement());78 var getInterceptors = emitter.CreateMethod("GetInterceptors", typeof(IInterceptor[]));79 getInterceptors.CodeBuilder.AddStatement(80 new ReturnStatement(interceptorsField));81 }82#if FEATURE_SERIALIZATION83 protected void ImplementGetObjectData(ClassEmitter emitter)84 {85 var getObjectData = emitter.CreateMethod("GetObjectData", typeof(void),86 new[] { typeof(SerializationInfo), typeof(StreamingContext) });87 var info = getObjectData.Arguments[0];88 var typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type));89 getObjectData.CodeBuilder.AddStatement(90 new AssignStatement(91 typeLocal,92 new MethodInvocationExpression(93 null,94 TypeMethods.StaticGetType,95 new ConstReference(typeof(ProxyObjectReference).AssemblyQualifiedName).ToExpression(),96 new ConstReference(1).ToExpression(),97 new ConstReference(0).ToExpression())));98 getObjectData.CodeBuilder.AddStatement(99 new ExpressionStatement(100 new MethodInvocationExpression(101 info,102 SerializationInfoMethods.SetType,103 typeLocal.ToExpression())));104 foreach (var field in emitter.GetAllFields())105 {106 if (field.Reference.IsStatic)107 {108 continue;109 }110 if (field.Reference.IsNotSerialized)111 {112 continue;113 }114 AddAddValueInvocation(info, getObjectData, field);115 }116 var interfacesLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(string[]));117 getObjectData.CodeBuilder.AddStatement(118 new AssignStatement(119 interfacesLocal,120 new NewArrayExpression(interfaces.Length, typeof(string))));121 for (var i = 0; i < interfaces.Length; i++)122 {123 getObjectData.CodeBuilder.AddStatement(124 new AssignArrayStatement(125 interfacesLocal,126 i,127 new ConstReference(interfaces[i].AssemblyQualifiedName).ToExpression()));128 }129 getObjectData.CodeBuilder.AddStatement(130 new ExpressionStatement(131 new MethodInvocationExpression(132 info,133 SerializationInfoMethods.AddValue_Object,134 new ConstReference("__interfaces").ToExpression(),135 interfacesLocal.ToExpression())));136 getObjectData.CodeBuilder.AddStatement(137 new ExpressionStatement(138 new MethodInvocationExpression(139 info,140 SerializationInfoMethods.AddValue_Object,141 new ConstReference("__baseType").ToExpression(),142 new ConstReference(emitter.BaseType.AssemblyQualifiedName).ToExpression())));143 getObjectData.CodeBuilder.AddStatement(144 new ExpressionStatement(145 new MethodInvocationExpression(146 info,147 SerializationInfoMethods.AddValue_Object,148 new ConstReference("__proxyGenerationOptions").ToExpression(),149 emitter.GetField("proxyGenerationOptions").ToExpression())));150 getObjectData.CodeBuilder.AddStatement(151 new ExpressionStatement(152 new MethodInvocationExpression(info,153 SerializationInfoMethods.AddValue_Object,154 new ConstReference("__proxyTypeId").ToExpression(),155 new ConstReference(proxyTypeId).ToExpression())));156 CustomizeGetObjectData(getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter);157 getObjectData.CodeBuilder.AddStatement(new ReturnStatement());158 }159 protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,160 FieldReference field)161 {162 getObjectData.CodeBuilder.AddStatement(163 new ExpressionStatement(164 new MethodInvocationExpression(165 serializationInfo,166 SerializationInfoMethods.AddValue_Object,167 new ConstReference(field.Reference.Name).ToExpression(),168 field.ToExpression())));169 return;170 }171 protected abstract void CustomizeGetObjectData(AbstractCodeBuilder builder, ArgumentReference serializationInfo,172 ArgumentReference streamingContext, ClassEmitter emitter);173#endif174 public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)175 {176 }177 }178}...
ProxyGenerationOptions.cs
Source:ProxyGenerationOptions.cs
...82#if FEATURE_SERIALIZATION83#if FEATURE_SECURITY_PERMISSIONS && DOTNET4084 [SecurityCritical]85#endif86 public void GetObjectData(SerializationInfo info, StreamingContext context)87 {88 info.AddValue("hook", Hook);89 info.AddValue("selector", Selector);90 info.AddValue("mixins", mixins);91 info.AddValue("baseTypeForInterfaceProxy.AssemblyQualifiedName", BaseTypeForInterfaceProxy.AssemblyQualifiedName);92 }93#endif94 public IProxyGenerationHook Hook { get; set; }95 public IInterceptorSelector Selector { get; set; }96 public Type BaseTypeForInterfaceProxy { get; set; }97 public IList<CustomAttributeInfo> AdditionalAttributes98 {99 get { return additionalAttributes; }100 }...
GetObjectData
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core;7using Telerik.JustMock.Core.Castle.DynamicProxy;8{9 {10 static void Main(string[] args)11 {12 var proxyGenerationOptions = new ProxyGenerationOptions();13 var serializationInfo = new System.Runtime.Serialization.SerializationInfo(typeof(ProxyGenerationOptions), new System.Runtime.Serialization.FormatterConverter());14 proxyGenerationOptions.GetObjectData(serializationInfo, new System.Runtime.Serialization.StreamingContext());15 Console.WriteLine("done");16 Console.ReadLine();17 }18 }19}20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25using Telerik.JustMock.Core;26using Telerik.JustMock.Core.Castle.DynamicProxy;27{28 {29 static void Main(string[] args)30 {31 var proxyGenerationOptions = new ProxyGenerationOptions();32 var serializationInfo = new System.Runtime.Serialization.SerializationInfo(typeof(ProxyGenerationOptions), new System.Runtime.Serialization.FormatterConverter());33 proxyGenerationOptions.GetObjectData(serializationInfo, new System.Runtime.Serialization.StreamingContext());34 Console.WriteLine("done");35 Console.ReadLine();36 }37 }38}39using System;40using System.Collections.Generic;41using System.Linq;42using System.Text;43using System.Threading.Tasks;44using Telerik.JustMock.Core;45using Telerik.JustMock.Core.Castle.DynamicProxy;46{47 {48 static void Main(string[] args)49 {50 var proxyGenerationOptions = new ProxyGenerationOptions();51 var serializationInfo = new System.Runtime.Serialization.SerializationInfo(typeof(ProxyGenerationOptions), new System.Runtime.Serialization.FormatterConverter());52 proxyGenerationOptions.GetObjectData(serializationInfo, new System.Runtime.Serialization.StreamingContext());53 Console.WriteLine("done");54 Console.ReadLine();55 }56 }57}58using System;59using System.Collections.Generic;60using System.Linq;61using System.Text;62using System.Threading.Tasks;
GetObjectData
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;7using System.Reflection;8{9 {10 static void Main(string[] args)11 {12 ProxyGenerationOptions options = new ProxyGenerationOptions();13 options.AddMixinInstance(new Mixin());14 options.AddMixinInstance(new Mixin2());15 options.AddMixinInstance(new Mixin3());16 options.AddMixinInstance(new Mixin4());17 options.AddMixinInstance(new Mixin5());18 options.AddMixinInstance(new Mixin6());19 options.AddMixinInstance(new Mixin7());20 options.AddMixinInstance(new Mixin8());21 options.AddMixinInstance(new Mixin9());22 options.AddMixinInstance(new Mixin10());23 options.AddMixinInstance(new Mixin11());24 options.AddMixinInstance(new Mixin12());25 options.AddMixinInstance(new Mixin13());26 options.AddMixinInstance(new Mixin14());27 options.AddMixinInstance(new Mixin15());28 options.AddMixinInstance(new Mixin16());29 options.AddMixinInstance(new Mixin17());30 options.AddMixinInstance(new Mixin18());31 options.AddMixinInstance(new Mixin19());32 options.AddMixinInstance(new Mixin20());33 options.AddMixinInstance(new Mixin21());34 options.AddMixinInstance(new Mixin22());35 options.AddMixinInstance(new Mixin23());36 options.AddMixinInstance(new Mixin24());37 options.AddMixinInstance(new Mixin25());38 options.AddMixinInstance(new Mixin26());39 options.AddMixinInstance(new Mixin27());40 options.AddMixinInstance(new Mixin28());41 options.AddMixinInstance(new Mixin29());42 options.AddMixinInstance(new Mixin30());43 options.AddMixinInstance(new Mixin31());44 options.AddMixinInstance(new Mixin32());45 options.AddMixinInstance(new Mixin33());46 options.AddMixinInstance(new Mixin34());47 options.AddMixinInstance(new Mixin35());48 options.AddMixinInstance(new Mixin36());49 options.AddMixinInstance(new Mixin37());50 options.AddMixinInstance(new Mixin38());51 options.AddMixinInstance(new Mixin39());52 options.AddMixinInstance(new Mixin40());53 options.AddMixinInstance(new Mixin41());54 options.AddMixinInstance(new Mixin42());55 options.AddMixinInstance(new Mixin43());
GetObjectData
Using AI Code Generation
1{2 public void Method1()3 {4 Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions obj = new Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions();5 System.Runtime.Serialization.SerializationInfo info = null;6 System.Runtime.Serialization.StreamingContext context = default(System.Runtime.Serialization.StreamingContext);7 obj.GetObjectData(info, context);8 }9}10{11 public void Method1()12 {13 Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions obj = new Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions();14 System.Runtime.Serialization.SerializationInfo info = null;15 System.Runtime.Serialization.StreamingContext context = default(System.Runtime.Serialization.StreamingContext);16 obj.GetObjectData(info, context);17 }18}
GetObjectData
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;7using Telerik.JustMock.Helpers;8{9 {10 static void Main(string[] args)11 {12 var proxyGenerationOptions = new ProxyGenerationOptions();13 var serializationInfo = Mock.Create<SerializationInfo>();14 var streamingContext = Mock.Create<StreamingContext>();15 proxyGenerationOptions.GetObjectData(serializationInfo, streamingContext);16 }17 }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24using Telerik.JustMock.Core.Castle.DynamicProxy;25using Telerik.JustMock.Helpers;26{27 {28 static void Main(string[] args)29 {30 var proxyGenerationOptions = new ProxyGenerationOptions();31 var serializationInfo = Mock.Create<SerializationInfo>();32 var streamingContext = Mock.Create<StreamingContext>();33 proxyGenerationOptions.GetObjectData(serializationInfo, streamingContext);34 }35 }36}37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42using Telerik.JustMock.Core.Castle.DynamicProxy;43using Telerik.JustMock.Helpers;44{45 {46 static void Main(string[] args)47 {48 var proxyGenerationOptions = new ProxyGenerationOptions();49 var serializationInfo = Mock.Create<SerializationInfo>();50 var streamingContext = Mock.Create<StreamingContext>();51 proxyGenerationOptions.GetObjectData(serializationInfo, streamingContext);52 }53 }54}55using System;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60using Telerik.JustMock.Core.Castle.DynamicProxy;61using Telerik.JustMock.Helpers;62{63 {64 static void Main(string[] args)65 {66 var proxyGenerationOptions = new ProxyGenerationOptions();
GetObjectData
Using AI Code Generation
1using Telerik.JustMock.Core.Castle.DynamicProxy;2{3 {4 public static void Main(string[] args)5 {6 ProxyGenerationOptions options = new ProxyGenerationOptions();7 options.AddMixinInstance(new System.Exception());
GetObjectData
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;7{8 {9 public void TestMethod()10 {11 SerializationInfo info = new SerializationInfo(typeof(TestClass), new FormatterConverter());12 ProxyGenerationOptions options = new ProxyGenerationOptions();13 options.GetObjectData(info, new StreamingContext());14 }15 }16}17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Runtime.Serialization;22using Telerik.JustMock.Core.Castle.DynamicProxy;23{24 {25 public void TestMethod()26 {27 SerializationInfo info = new SerializationInfo(typeof(TestClass), new FormatterConverter());28 ProxyGenerationOptions options = new ProxyGenerationOptions();29 options.GetObjectData(info, new StreamingContext());30 }31 }32}33using System;34using System.Collections.Generic;35using System.Linq;36using System.Text;37using System.Runtime.Serialization;38using Telerik.JustMock.Core.Castle.DynamicProxy;39{40 {41 public void TestMethod()42 {43 SerializationInfo info = new SerializationInfo(typeof(TestClass), new FormatterConverter());44 ProxyGenerationOptions options = new ProxyGenerationOptions();45 options.GetObjectData(info, new StreamingContext());46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Runtime.Serialization;54using Telerik.JustMock.Core.Castle.DynamicProxy;55{56 {57 public void TestMethod()58 {59 SerializationInfo info = new SerializationInfo(typeof(TestClass), new FormatterConverter());
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!!