How to use ProxyGenerationOptions class of Telerik.JustMock.Core.Castle.DynamicProxy package

Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerationOptions

ClassProxyGenerator.cs

Source:ClassProxyGenerator.cs Github

copy

Full Screen

...28		{29			CheckNotGenericTypeDefinition(targetType, "targetType");30			EnsureDoesNotImplementIProxyTargetAccessor(targetType, "targetType");31		}32		public Type GenerateCode(Type[] interfaces, ProxyGenerationOptions options)33		{34			// make sure ProxyGenerationOptions is initialized35			options.Initialize();36			interfaces = TypeUtil.GetAllInterfaces(interfaces);37			CheckNotGenericTypeDefinitions(interfaces, "interfaces");38			ProxyGenerationOptions = options;39			var cacheKey = new CacheKey(targetType, interfaces, options);40			return ObtainProxyType(cacheKey, (n, s) => GenerateType(n, interfaces, s));41		}42		protected virtual Type GenerateType(string name, Type[] interfaces, INamingScope namingScope)43		{44			IEnumerable<ITypeContributor> contributors;45			var implementedInterfaces = GetTypeImplementerMapping(interfaces, out contributors, namingScope);46			var model = new MetaType();47			// Collect methods48			foreach (var contributor in contributors)49			{50				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);51			}52			ProxyGenerationOptions.Hook.MethodsInspected();53			var emitter = BuildClassEmitter(name, targetType, implementedInterfaces);54			CreateFields(emitter);55			CreateTypeAttributes(emitter);56			// Constructor57			var cctor = GenerateStaticConstructor(emitter);58			var constructorArguments = new List<FieldReference>();59			foreach (var contributor in contributors)60			{61				contributor.Generate(emitter, ProxyGenerationOptions);62				// TODO: redo it63				var mixinContributor = contributor as MixinContributor;64				if (mixinContributor != null)65				{66					constructorArguments.AddRange(mixinContributor.Fields);67				}68			}69			// constructor arguments70			var interceptorsField = emitter.GetField("__interceptors");71			constructorArguments.Add(interceptorsField);72			var selector = emitter.GetField("__selector");73			if (selector != null)74			{75				constructorArguments.Add(selector);76			}77			GenerateConstructors(emitter, targetType, constructorArguments.ToArray());78			GenerateParameterlessConstructor(emitter, targetType, interceptorsField);79			// Complete type initializer code body80			CompleteInitCacheMethod(cctor.CodeBuilder);81            // Crosses fingers and build type82            Type proxyType = emitter.BuildType();83			InitializeStaticFields(proxyType);84			return proxyType;85		}86		protected virtual IEnumerable<Type> GetTypeImplementerMapping(Type[] interfaces,87																	  out IEnumerable<ITypeContributor> contributors,88																	  INamingScope namingScope)89		{90			var methodsToSkip = new List<MethodInfo>();91			var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class);92			// TODO: the trick with methodsToSkip is not very nice...93			var proxyTarget = new ClassProxyTargetContributor(targetType, methodsToSkip, namingScope) { Logger = Logger };94			IDictionary<Type, ITypeContributor> typeImplementerMapping = new Dictionary<Type, ITypeContributor>();95			// Order of interface precedence:96			// 1. first target97			// target is not an interface so we do nothing98			var targetInterfaces = targetType.GetAllInterfaces();99			var additionalInterfaces = TypeUtil.GetAllInterfaces(interfaces);100			// 2. then mixins101			var mixins = new MixinContributor(namingScope, false) { Logger = Logger };102			if (ProxyGenerationOptions.HasMixins)103			{104				foreach (var mixinInterface in ProxyGenerationOptions.MixinData.MixinInterfaces)105				{106					if (targetInterfaces.Contains(mixinInterface))107					{108						// OK, so the target implements this interface. We now do one of two things:109						if (additionalInterfaces.Contains(mixinInterface) && typeImplementerMapping.ContainsKey(mixinInterface) == false)110						{111							AddMappingNoCheck(mixinInterface, proxyTarget, typeImplementerMapping);112							proxyTarget.AddInterfaceToProxy(mixinInterface);113						}114						// we do not intercept the interface115						mixins.AddEmptyInterface(mixinInterface);116					}117					else118					{119						if (!typeImplementerMapping.ContainsKey(mixinInterface))120						{121							mixins.AddInterfaceToProxy(mixinInterface);122							AddMappingNoCheck(mixinInterface, mixins, typeImplementerMapping);123						}124					}125				}126			}127			var additionalInterfacesContributor = new InterfaceProxyWithoutTargetContributor(namingScope,128																							 (c, m) => NullExpression.Instance)129			{ Logger = Logger };130			// 3. then additional interfaces131			foreach (var @interface in additionalInterfaces)132			{133				if (targetInterfaces.Contains(@interface))134				{135					if (typeImplementerMapping.ContainsKey(@interface))136					{137						continue;138					}139					// we intercept the interface, and forward calls to the target type140					AddMappingNoCheck(@interface, proxyTarget, typeImplementerMapping);141					proxyTarget.AddInterfaceToProxy(@interface);142				}143				else if (ProxyGenerationOptions.MixinData.ContainsMixin(@interface) == false)144				{145					additionalInterfacesContributor.AddInterfaceToProxy(@interface);146					AddMapping(@interface, additionalInterfacesContributor, typeImplementerMapping);147				}148			}149			// 4. plus special interfaces150#if FEATURE_SERIALIZATION151			if (targetType.IsSerializable)152			{153				AddMappingForISerializable(typeImplementerMapping, proxyInstance);154			}155#endif156			try157			{...

Full Screen

Full Screen

DelegateProxyGenerator.cs

Source:DelegateProxyGenerator.cs Github

copy

Full Screen

...26	internal class DelegateProxyGenerator : BaseProxyGenerator27	{28		public DelegateProxyGenerator(ModuleScope scope, Type delegateType) : base(scope, delegateType)29		{30			ProxyGenerationOptions = new ProxyGenerationOptions(new DelegateProxyGenerationHook());31			ProxyGenerationOptions.Initialize();32		}33		public Type GetProxyType()34		{35			var cacheKey = new CacheKey(targetType, null, null);36			return ObtainProxyType(cacheKey, GenerateType);37		}38		protected virtual IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContributor> contributors,39		                                                              INamingScope namingScope)40		{41			var methodsToSkip = new List<MethodInfo>();42			var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, Type.EmptyTypes,43			                                                      ProxyTypeConstants.ClassWithTarget);44			var proxyTarget = new DelegateProxyTargetContributor(targetType, namingScope) { Logger = Logger };45			IDictionary<Type, ITypeContributor> typeImplementerMapping = new Dictionary<Type, ITypeContributor>();46			// Order of interface precedence:47			// 1. first target, target is not an interface so we do nothing48			// 2. then mixins - we support none so we do nothing49			// 3. then additional interfaces - we support none so we do nothing50			// 4. plus special interfaces51#if FEATURE_SERIALIZATION52			if (targetType.IsSerializable)53			{54				AddMappingForISerializable(typeImplementerMapping, proxyInstance);55			}56#endif57			AddMappingNoCheck(typeof(IProxyTargetAccessor), proxyInstance, typeImplementerMapping);58			contributors = new List<ITypeContributor>59			{60				proxyTarget,61				proxyInstance62			};63			return typeImplementerMapping.Keys;64		}65		private FieldReference CreateTargetField(ClassEmitter emitter)66		{67			var targetField = emitter.CreateField("__target", targetType);68#if FEATURE_SERIALIZATION69			emitter.DefineCustomAttributeFor<XmlIgnoreAttribute>(targetField);70#endif71			return targetField;72		}73		private Type GenerateType(string name, INamingScope namingScope)74		{75			IEnumerable<ITypeContributor> contributors;76			var implementedInterfaces = GetTypeImplementerMapping(out contributors, namingScope);77			var model = new MetaType();78			// Collect methods79			foreach (var contributor in contributors)80			{81				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);82			}83			ProxyGenerationOptions.Hook.MethodsInspected();84			var emitter = BuildClassEmitter(name, typeof(object), implementedInterfaces);85			CreateFields(emitter);86			CreateTypeAttributes(emitter);87			// Constructor88			var cctor = GenerateStaticConstructor(emitter);89			var targetField = CreateTargetField(emitter);90			var constructorArguments = new List<FieldReference> { targetField };91			foreach (var contributor in contributors)92			{93				contributor.Generate(emitter, ProxyGenerationOptions);94			}95			// constructor arguments96			var interceptorsField = emitter.GetField("__interceptors");97			constructorArguments.Add(interceptorsField);98			var selector = emitter.GetField("__selector");99			if (selector != null)100			{101				constructorArguments.Add(selector);102			}103			GenerateConstructor(emitter, null, constructorArguments.ToArray());104			GenerateParameterlessConstructor(emitter, targetType, interceptorsField);105			// Complete type initializer code body106			CompleteInitCacheMethod(cctor.CodeBuilder);107			// Crosses fingers and build type...

Full Screen

Full Screen

InterfaceProxyWithoutTargetGenerator.cs

Source:InterfaceProxyWithoutTargetGenerator.cs Github

copy

Full Screen

...51			var model = new MetaType();52			// collect elements53			foreach (var contributor in contributors)54			{55				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);56			}57			ProxyGenerationOptions.Hook.MethodsInspected();58			ClassEmitter emitter;59			FieldReference interceptorsField;60			var baseType = Init(typeName, out emitter, proxyTargetType, out interceptorsField, allInterfaces);61			// Constructor62			var cctor = GenerateStaticConstructor(emitter);63			var mixinFieldsList = new List<FieldReference>();64			foreach (var contributor in contributors)65			{66				contributor.Generate(emitter, ProxyGenerationOptions);67				// TODO: redo it68				if (contributor is MixinContributor)69				{70					mixinFieldsList.AddRange((contributor as MixinContributor).Fields);71				}72			}73			var ctorArguments = new List<FieldReference>(mixinFieldsList) { interceptorsField, targetField };74			var selector = emitter.GetField("__selector");75			if (selector != null)76			{77				ctorArguments.Add(selector);78			}79			GenerateConstructors(emitter, baseType, ctorArguments.ToArray());80			// Complete type initializer code body...

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;3using Telerik.JustMock.Core.Castle.Core;4using Telerik.JustMock.Core.Castle.Core.Internal;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;6using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;7using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.CodeBuilders;8using System.Reflection.Emit;9using System.Reflection;10using System;11{12public static void Main()13{14ProxyGenerationOptions options = new ProxyGenerationOptions();15ProxyGenerator proxyGenerator = new ProxyGenerator();16MyInterceptor myInterceptor = new MyInterceptor();17MyInterceptorSelector myInterceptorSelector = new MyInterceptorSelector(myInterceptor);18options.Selector = myInterceptorSelector;19var myClass = proxyGenerator.CreateInterfaceProxyWithoutTarget<IMyInterface>(options);20myClass.MyMethod();21}22}23{24public void Intercept(IInvocation invocation)25{26Console.WriteLine("Before invocation of the method: {0}", invocation.Method.Name);27invocation.Proceed();28Console.WriteLine("After invocation of the method: {0}", invocation.Method.Name);29}30}31{32private IInterceptor[] interceptors;33public MyInterceptorSelector(IInterceptor interceptor)34{35interceptors = new IInterceptor[] { interceptor };36}37public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)38{39return this.interceptors;40}41}42{43void MyMethod();44}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;3using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;4using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;6using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;7using Telerik.JustMock.Core.Castle.DynamicProxy.Internal.Caching;8using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;9using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;10using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;11using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;12using System;13using System.Collections.Generic;14using System.Linq;15using System.Text;16using System.Threading.Tasks;17{18    {19        public static void Main()20        {21            ProxyGenerator generator = new ProxyGenerator();22            ProxyGenerationOptions options = new ProxyGenerationOptions();23            options.Hook = new MockingProxy();24            var proxy = generator.CreateClassProxy(typeof(RealClass), options);25            proxy.DoSomething();26        }27    }28    {29        public virtual void DoSomething()30        {31            Console.WriteLine("RealClass.DoSomething");32        }33    }34}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8    {9        public static ProxyGenerationOptions Default { get; set; }10    }11}12using Telerik.JustMock.Core.Castle.DynamicProxy;13using System;14using System.Collections.Generic;15using System.Linq;16using System.Text;17using System.Threading.Tasks;18{19    {20        public static ProxyGenerationOptions Default { get; set; }21    }22}23using Telerik.JustMock.Core.Castle.DynamicProxy;24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29{30    {31        public static ProxyGenerationOptions Default { get; set; }32    }33}34using Telerik.JustMock.Core.Castle.DynamicProxy;35using System;36using System.Collections.Generic;37using System.Linq;38using System.Text;39using System.Threading.Tasks;40{41    {42        public static ProxyGenerationOptions Default { get; set; }43    }44}45using Telerik.JustMock.Core.Castle.DynamicProxy;46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51{52    {53        public static ProxyGenerationOptions Default { get; set; }54    }55}56using Telerik.JustMock.Core.Castle.DynamicProxy;57using System;58using System.Collections.Generic;59using System.Linq;60using System.Text;61using System.Threading.Tasks;62{

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2using Telerik.JustMock;3{4    {5        public virtual string Method1()6        {7            return "Hello World!";8        }9    }10    {11        public virtual string Method2()12        {13            return "Hello World!";14        }15    }16    {17        string Method1();18    }19    {20        string Method2();21    }22    {23        public virtual string Method3()24        {25            return "Hello World!";26        }27    }28    {29        public virtual string Method4()30        {31            return "Hello World!";32        }33    }34    {35        string Method3();36    }37    {38        string Method4();39    }40    {41        public virtual string Method5()42        {43            return "Hello World!";44        }45    }46    {47        public virtual string Method6()48        {49            return "Hello World!";50        }51    }52    {53        string Method5();54    }55    {56        string Method6();57    }58    {59        public virtual string Method7()60        {61            return "Hello World!";62        }63    }64    {65        public virtual string Method8()66        {67            return "Hello World!";68        }69    }70    {71        string Method7();72    }73    {74        string Method8();75    }76    {77        public virtual string Method9()78        {79            return "Hello World!";80        }81    }82    {83        public virtual string Method10()84        {85            return "Hello World!";86        }87    }88    {89        string Method9();90    }91    {92        string Method10();93    }94    {95        public virtual string Method11()96        {97            return "Hello World!";98        }99    }

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core.Castle.DynamicProxy;4{5    {6        public void Foo()7        {8            var options = new ProxyGenerationOptions();9            var mock = Mock.Create<IFoo>(options);10        }11    }12    {13    }14}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2{3    public void Method1()4    {5        var options = new ProxyGenerationOptions();6        options.Hook = new ProxyGenerationHook();7    }8}9using Telerik.JustMock.Core.Castle.Core;10{11    public void Method2()12    {13        var options = new ProxyGenerationOptions();14        options.Hook = new ProxyGenerationHook();15    }16}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2using Telerik.JustMock.Core;3using Telerik.JustMock.Helpers;4{5    public virtual int Bar()6    {7        return 1;8    }9}10{11    public void Foo()12    {13        var foo = Mock.Create<Foo>();14        var options = new ProxyGenerationOptions();15        options.AddMixinInstance(new Foo());16        Mock.Arrange(() => foo.Bar()).Returns(2);17    }18}19using Telerik.JustMock.Castle.DynamicProxy;20using Telerik.JustMock.Core;21using Telerik.JustMock.Helpers;22{23    public virtual int Bar()24    {25        return 1;26    }27}28{29    public void Foo()30    {31        var foo = Mock.Create<Foo>();32        var options = new ProxyGenerationOptions();33        options.AddMixinInstance(new Foo());34        Mock.Arrange(() => foo.Bar()).Returns(2);35    }36}37using Telerik.JustMock.Castle.DynamicProxy;38using Telerik.JustMock.Core;39using Telerik.JustMock.Helpers;40{41    public virtual int Bar()42    {43        return 1;44    }45}46{47    public void Foo()48    {49        var foo = Mock.Create<Foo>();50        var options = new ProxyGenerationOptions();51        options.AddMixinInstance(new Foo());52        Mock.Arrange(() => foo.Bar()).Returns(2);53    }54}55using Telerik.JustMock.Core.Castle.DynamicProxy;56using Telerik.JustMock.Core;57using Telerik.JustMock.Helpers;58{59    public virtual int Bar()60    {61        return 1;62    }63}64{65    public void Foo()66    {67        var foo = Mock.Create<Foo>();68        var options = new ProxyGenerationOptions();69        options.AddMixinInstance(new Foo());70        Mock.Arrange(() => foo.Bar()).Returns(2);71    }72}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy;2{3    {4        public void TestMethod()5        {6            var proxyGenerationOptions = new ProxyGenerationOptions();7        }8    }9}10using Telerik.JustMock.Core.Castle.Core;11{12    {13        public void TestMethod()14        {15            var proxyGenerationOptions = new ProxyGenerationOptions();16        }17    }18}19using Telerik.JustMock.Core.Castle.DynamicProxy;20{21    {22        public void TestMethod()23        {24            var proxyGenerationOptions = new ProxyGenerationOptions();25        }26    }27}

Full Screen

Full Screen

ProxyGenerationOptions

Using AI Code Generation

copy

Full Screen

1var options = new ProxyGenerationOptions();2options.AddMixinInstance(new MockInterceptor());3options.SetProxyTargetType(typeof(IInterface));4var proxy = Mock.Create<IInterface>(options);5var interceptor = new MockInterceptor();6var proxy = Mock.Create<IInterface>(interceptor);7var interceptor = new MockInterceptor();8var proxy = Mock.Create<IClass>(interceptor);9var interceptor = new MockInterceptor();10var proxy = Mock.Create<IAbstract>(interceptor);11var interceptor = new MockInterceptor();12var proxy = Mock.Create<ISealed>(interceptor);13var options = new ProxyGenerationOptions();14options.AddMixinInstance(new MockInterceptor());15options.SetProxyTargetType(typeof(IInterface));16var proxy = Mock.Create<IInterface>(options);17var interceptor = new MockInterceptor();18var proxy = Mock.Create<IInterface>(interceptor);19var interceptor = new MockInterceptor();20var proxy = Mock.Create<IClass>(interceptor);21var interceptor = new MockInterceptor();22var proxy = Mock.Create<IAbstract>(interceptor);23var interceptor = new MockInterceptor();24var proxy = Mock.Create<ISealed>(interceptor);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful