Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.ImplementedByTarget
InterfaceProxyWithTargetGenerator.cs
Source:InterfaceProxyWithTargetGenerator.cs  
...69				AddMappingNoCheck(@interface, contributor, typeImplementerMapping);70			}71			foreach (var @interface in additionalInterfaces)72			{73				if (!ImplementedByTarget(targetInterfaces, @interface) || proxiedInterfaces.Contains(@interface))74				{75					continue;76				}77				contributor.AddInterfaceToProxy(@interface);78				AddMappingNoCheck(@interface, contributor, typeImplementerMapping);79			}80			return contributor;81		}82#if FEATURE_SERIALIZATION83		protected override void CreateTypeAttributes(ClassEmitter emitter)84		{85			base.CreateTypeAttributes(emitter);86			emitter.DefineCustomAttribute<SerializableAttribute>();87		}88#endif89		protected virtual Type GenerateType(string typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)90		{91			IEnumerable<ITypeContributor> contributors;92			var allInterfaces = GetTypeImplementerMapping(interfaces, proxyTargetType, out contributors, namingScope);93			ClassEmitter emitter;94			FieldReference interceptorsField;95			var baseType = Init(typeName, out emitter, proxyTargetType, out interceptorsField, allInterfaces);96			var model = new MetaType();97			// Collect methods98			foreach (var contributor in contributors)99			{100				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);101			}102			ProxyGenerationOptions.Hook.MethodsInspected();103			// Constructor104			var cctor = GenerateStaticConstructor(emitter);105			var ctorArguments = new List<FieldReference>();106			foreach (var contributor in contributors)107			{108				contributor.Generate(emitter, ProxyGenerationOptions);109				// TODO: redo it110				if (contributor is MixinContributor)111				{112					ctorArguments.AddRange((contributor as MixinContributor).Fields);113				}114			}115			ctorArguments.Add(interceptorsField);116			ctorArguments.Add(targetField);117			var selector = emitter.GetField("__selector");118			if (selector != null)119			{120				ctorArguments.Add(selector);121			}122			GenerateConstructors(emitter, baseType, ctorArguments.ToArray());123			// Complete type initializer code body124			CompleteInitCacheMethod(cctor.CodeBuilder);125			// Crosses fingers and build type126			var generatedType = emitter.BuildType();127			InitializeStaticFields(generatedType);128			return generatedType;129		}130		protected virtual InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces(131			INamingScope namingScope)132		{133			return new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger };134		}135		protected virtual IEnumerable<Type> GetTypeImplementerMapping(Type[] interfaces, Type proxyTargetType,136		                                                              out IEnumerable<ITypeContributor> contributors,137		                                                              INamingScope namingScope)138		{139			IDictionary<Type, ITypeContributor> typeImplementerMapping = new Dictionary<Type, ITypeContributor>();140			var mixins = new MixinContributor(namingScope, AllowChangeTarget) { Logger = Logger };141			// Order of interface precedence:142			// 1. first target143			var targetInterfaces = proxyTargetType.GetAllInterfaces();144			var additionalInterfaces = TypeUtil.GetAllInterfaces(interfaces);145			var target = AddMappingForTargetType(typeImplementerMapping, proxyTargetType, targetInterfaces, additionalInterfaces,146			                                     namingScope);147			// 2. then mixins148			if (ProxyGenerationOptions.HasMixins)149			{150				foreach (var mixinInterface in ProxyGenerationOptions.MixinData.MixinInterfaces)151				{152					if (targetInterfaces.Contains(mixinInterface))153					{154						// OK, so the target implements this interface. We now do one of two things:155						if (additionalInterfaces.Contains(mixinInterface))156						{157							// we intercept the interface, and forward calls to the target type158							AddMapping(mixinInterface, target, typeImplementerMapping);159						}160						// we do not intercept the interface161						mixins.AddEmptyInterface(mixinInterface);162					}163					else164					{165						if (!typeImplementerMapping.ContainsKey(mixinInterface))166						{167							mixins.AddInterfaceToProxy(mixinInterface);168							typeImplementerMapping.Add(mixinInterface, mixins);169						}170					}171				}172			}173			var additionalInterfacesContributor = GetContributorForAdditionalInterfaces(namingScope);174			// 3. then additional interfaces175			foreach (var @interface in additionalInterfaces)176			{177				if (typeImplementerMapping.ContainsKey(@interface))178				{179					continue;180				}181				if (ProxyGenerationOptions.MixinData.ContainsMixin(@interface))182				{183					continue;184				}185				additionalInterfacesContributor.AddInterfaceToProxy(@interface);186				AddMappingNoCheck(@interface, additionalInterfacesContributor, typeImplementerMapping);187			}188			// 4. plus special interfaces189			var instance = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces);190#if FEATURE_SERIALIZATION191			AddMappingForISerializable(typeImplementerMapping, instance);192#endif193			try194			{195				AddMappingNoCheck(typeof(IProxyTargetAccessor), instance, typeImplementerMapping);196			}197			catch (ArgumentException)198			{199				HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces, additionalInterfaces);200			}201			contributors = new List<ITypeContributor>202			{203				target,204				additionalInterfacesContributor,205				mixins,206				instance207			};208			return typeImplementerMapping.Keys;209		}210		protected virtual Type Init(string typeName, out ClassEmitter emitter, Type proxyTargetType,211		                            out FieldReference interceptorsField, IEnumerable<Type> interfaces)212		{213			var baseType = ProxyGenerationOptions.BaseTypeForInterfaceProxy;214			emitter = BuildClassEmitter(typeName, baseType, interfaces);215			CreateFields(emitter, proxyTargetType);216			CreateTypeAttributes(emitter);217			interceptorsField = emitter.GetField("__interceptors");218			return baseType;219		}220		private void CreateFields(ClassEmitter emitter, Type proxyTargetType)221		{222			base.CreateFields(emitter);223			targetField = emitter.CreateField("__target", proxyTargetType);224#if FEATURE_SERIALIZATION225			emitter.DefineCustomAttributeFor<XmlIgnoreAttribute>(targetField);226#endif227		}228		private void EnsureValidBaseType(Type type)229		{230			if (type == null)231			{232				throw new ArgumentException(233					"Base type for proxy is null reference. Please set it to System.Object or some other valid type.");234			}235			if (!type.GetTypeInfo().IsClass)236			{237				ThrowInvalidBaseType(type, "it is not a class type");238			}239			if (type.GetTypeInfo().IsSealed)240			{241				ThrowInvalidBaseType(type, "it is sealed");242			}243			var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,244			                                      null, Type.EmptyTypes, null);245			if (constructor == null || constructor.IsPrivate)246			{247				ThrowInvalidBaseType(type, "it does not have accessible parameterless constructor");248			}249		}250		private bool ImplementedByTarget(ICollection<Type> targetInterfaces, Type @interface)251		{252			return targetInterfaces.Contains(@interface);253		}254		private void ThrowInvalidBaseType(Type type, string doesNotHaveAccessibleParameterlessConstructor)255		{256			var format =257				"Type {0} is not valid base type for interface proxy, because {1}. Only a non-sealed class with non-private default constructor can be used as base type for interface proxy. Please use some other valid type.";258			throw new ArgumentException(string.Format(format, type, doesNotHaveAccessibleParameterlessConstructor));259		}260	}261}...ImplementedByTarget
Using AI Code Generation
1{2    {3        public InterfaceProxyWithTargetGenerator(ModuleScope scope, ProxyGenerationOptions options) : base(scope, options)4        {5        }6        protected override ProxyGenerator CreateProxyGenerator(ModuleScope scope)7        {8            return new InterfaceProxyWithTargetGenerator(scope, Options);9        }10        public override Type GenerateCode(Type[] interfaces, Type targetType)11        {12            if (interfaces == null)13            {14                throw new ArgumentNullException("interfaces");15            }16            if (targetType == null)17            {18                throw new ArgumentNullException("targetType");19            }20            if (interfaces.Length == 0)21            {22                throw new ArgumentException("You must specify at least one interface.", "interfaces");23            }24            var proxyType = base.GenerateCode(interfaces, targetType);25            return proxyType;26        }27    }28}29{30    {31        public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, ProxyGenerationOptions options) : base(scope, options)32        {33        }34        protected override ProxyGenerator CreateProxyGenerator(ModuleScope scope)35        {36            return new InterfaceProxyWithTargetInterfaceGenerator(scope, Options);37        }38        public override Type GenerateCode(Type[] interfaces, Type targetType)39        {40            if (interfaces == null)41            {42                throw new ArgumentNullException("interfaces");43            }44            if (targetType == null)45            {46                throw new ArgumentNullException("targetType");47            }48            if (interfaces.Length == 0)49            {50                throw new ArgumentException("You must specify at least one interface.", "interfaces");51            }52            var proxyType = base.GenerateCode(interfaces, targetType);53            return proxyType;54        }55    }56}57{58    {ImplementedByTarget
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Core;8using Telerik.JustMock.Helpers;9{10    {11        string GetString();12    }13    {14        public string GetString()15        {16            return "Hello World";17        }18    }19    {20        public static void Main()21        {22            var proxy = Mock.Create<IInterface>(Behavior.CallOriginal, new object[] { new Class() });23            Console.WriteLine(proxy.GetString());24        }25    }26}ImplementedByTarget
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Core;8using Telerik.JustMock.Helpers;9{10    {11        int MyMethod();12    }13    {14        public int MyMethod()15        {16            return 1;17        }18    }19    {20        static void Main(string[] args)21        {22            Mock.Arrange(() => Mock.Create<IMyInterface>(null, null, null)).Returns(new MyClass());23            var myInterface = Mock.Create<IMyInterface>(null, null, null);24            myInterface.MyMethod();25        }26    }27}ImplementedByTarget
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;6using Telerik.JustMock.Core.Castle.DynamicProxy;7using System.Reflection;8using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;9using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;10using Telerik.JustMock.Core.Castle.DynamicProxy.Contributors;11using System.Runtime.Serialization;12using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;13using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.CodeBuilders;14using System.Runtime.CompilerServices;15using System.Globalization;16{17    {18        public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces) : base(scope, interfaces)19        {20            if (targetType == null)21                throw new ArgumentNullException("targetType");22            if (targetType.IsInterface)23                throw new ArgumentException("Target type cannot be an interface.", "targetType");24            TargetType = targetType;25        }26        public Type TargetType { get; private set; }27        protected override void GenerateConstructors(ClassEmitter @class, ConstructorInfo[] constructors)28        {29            var targetField = @class.GetField("__target");30            var targetFieldReference = new FieldReference(targetField);31            var targetFieldReferenceExpression = new ReferenceExpression(targetFieldReference);32            var targetType = TargetType;33            var targetTypeReference = new TypeReference(targetType);34            var targetTypeReferenceExpression = new ReferenceExpression(targetTypeReference);35            var targetTypeReferenceExpression2 = new ReferenceExpression(targetTypeReference);36            var targetTypeReferenceExpression3 = new ReferenceExpression(targetTypeReference);37            var targetTypeReferenceExpression4 = new ReferenceExpression(targetTypeReference);38            var targetTypeReferenceExpression5 = new ReferenceExpression(targetTypeReference);39            var targetTypeReferenceExpression6 = new ReferenceExpression(targetTypeReference);40            var targetTypeReferenceExpression7 = new ReferenceExpression(targetTypeReference);41            var targetTypeReferenceExpression8 = new ReferenceExpression(targetTypeReference);42            var targetTypeReferenceExpression9 = new ReferenceExpression(targetTypeReference);43            var targetTypeReferenceExpression10 = new ReferenceExpression(targetTypeReference);44            var targetTypeReferenceExpression11 = new ReferenceExpression(targetTypeReference);45            var targetTypeReferenceExpression12 = new ReferenceExpression(targetTypeImplementedByTarget
Using AI Code Generation
1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core;4using Telerik.JustMock.Core.Castle.DynamicProxy;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;6{7    {8        void MyMethod();9    }10    {11        public void MyMethod()12        {13            Console.WriteLine("MyMethod");14        }15    }16    {17        public void MyMethod()18        {19            Console.WriteLine("MyMethod");20        }21    }22    {23        public void MyMethod()24        {25            Console.WriteLine("MyMethod");26        }27    }28    {29        public void MyMethod()30        {31            Console.WriteLine("MyMethod");32        }33    }34    {35        public static void Main()36        {37            var myClass = Mock.Create<MyClass>();38            var myClass2 = Mock.Create<MyClass2>();39            var myInterfaceImplementation = Mock.Create<MyInterfaceImplementation>();40            var myInterfaceImplementation2 = Mock.Create<MyInterfaceImplementation2>();41            var generator = new InterfaceProxyWithTargetGenerator();42            var proxy = generator.ImplementedByTarget(typeof(IMyInterface), myClass, myInterfaceImplementation);43            var proxy2 = generator.ImplementedByTarget(typeof(IMyInterface), myClass2, myInterfaceImplementation2);44            var myInterface = (IMyInterface)proxy;45            myInterface.MyMethod();46            var myInterface2 = (IMyInterface)proxy2;47            myInterface2.MyMethod();48        }49    }50}ImplementedByTarget
Using AI Code Generation
1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;4using Telerik.JustMock.Helpers;5{6    {7        void Test();8    }9    {10        public void Test()11        {12            var test = Mock.Create<ITest>();13            Mock.Arrange(() => test.Test()).DoNothing();14            test.Test();15        }16    }17    {18        public void Test()19        {20            var test = Mock.Create<ITest>();21            Mock.Arrange(() => test.Test()).DoNothing();22            test.Test();23        }24    }25    {26        static void Main(string[] args)27        {28            var test = new TestClass();29            test.Test();30            var test2 = new TestClass2();31            test2.Test();32        }33    }34}ImplementedByTarget
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock;7using Telerik.JustMock.Core;8using Telerik.JustMock.Helpers;9{10    {11        void Method();12    }13    {14        public void Method()15        {16            Mock.Create<IInterface>();17        }18    }19    {20        public void Method()21        {22            Mock.Create<IInterface>();23        }24    }25    {26        public void Method()27        {28            Mock.Create<IInterface>();29        }30    }31    {32        public void Method()33        {34            Mock.Create<IInterface>();35        }36    }37    {38        public void Method()39        {40            Mock.Create<IInterface>();41        }42    }43    {44        public void Method()45        {46            Mock.Create<IInterface>();47        }48    }49    {50        public void Method()51        {52            Mock.Create<IInterface>();53        }54    }55    {56        public void Method()57        {58            Mock.Create<IInterface>();59        }60    }61    {62        public void Method()63        {64            Mock.Create<IInterface>();65        }66    }67    {68        public void Method()69        {70            Mock.Create<IInterface>();71        }72    }73    {74        public void Method()75        {76            Mock.Create<IInterface>();77        }78    }79    {80        public void Method()81        {82            Mock.Create<IInterface>();83        }84    }85    {86        public void Method()87        {88            Mock.Create<IInterface>();89        }90    }91    {92        public void Method()93        {94            Mock.Create<IInterface>();95        }96    }97    {98        public void Method()99        {100            Mock.Create<IInterface>();101        }102    }103    {104        public void Method()105        {106            Mock.Create<IInterface>();107        }108    }109    {110        public void Method()111        {112            Mock.Create<IInterface>();113        }114    }ImplementedByTarget
Using AI Code Generation
1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core;4using Telerik.JustMock.Helpers;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;6{7    {8        static void Main(string[] args)9        {10            var proxy = InterfaceProxyWithTargetGenerator.ImplementedByTarget(typeof(IInterface), new Class(), null);11            var proxy2 = Mock.Create<IInterface>();12            Console.WriteLine(proxy2.GetType());13            Console.WriteLine(proxy.GetType());14            Console.ReadLine();15        }16    }17    {18        void Foo();19    }20    {21        public void Foo()22        {23            Console.WriteLine("Foo");24        }25    }26}27using System;28using Telerik.JustMock;29using Telerik.JustMock.Core;30using Telerik.JustMock.Helpers;31using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;32{33    {34        static void Main(string[] args)35        {36            var proxy = InterfaceProxyWithTargetGenerator.CreateProxy(typeof(IInterface), new Class(), null);37            var proxy2 = Mock.Create<IInterface>();38            Console.WriteLine(proxy2.GetType());39            Console.WriteLine(proxy.GetType());40            Console.ReadLine();41        }42    }43    {44        void Foo();45    }46    {47        public void Foo()48        {49            Console.WriteLine("Foo");50        }51    }52}53at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.ImplementedByTarget(Type interfaceToProxy, Object target, Type[] additionalInterfacesToProxy)54at ConsoleApplication1.Program.Main(String[] args) inImplementedByTarget
Using AI Code Generation
1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;4{5    {6        void Method1();7    }8    {9        public void Method1()10        {11            Console.WriteLine("Method1");12        }13    }14    {15        public void Method2()16        {17            Console.WriteLine("Method2");18        }19    }20    {21        static void Main(string[] args)22        {23            IInterface obj1 = Mock.Create<IInterface>();24            obj1.Method1();25            var obj2 = InterfaceProxyWithTargetGenerator.ImplementedByTarget<IInterface>(new Class1());26            obj2.Method1();27            var obj3 = InterfaceProxyWithTargetGenerator.ImplementedByTarget<IInterface>(new Class2());28            obj3.Method1();29        }30    }31}32using System;33using Telerik.JustMock;34using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;35{36    {37        void Method1();38    }39    {40        public void Method1()41        {42            Console.WriteLine("Method1");43        }44    }45    {46        public void Method2()47        {48            Console.WriteLine("Method2");49        }50    }51    {52        static void Main(string[] args)53        {54            IInterface obj1 = Mock.Create<IInterface>();55            obj1.Method1();56            var obj2 = InterfaceProxyWithTargetGenerator.ImplementedByTarget<IInterface>(new Class1());57            obj2.Method1();58            var obj3 = InterfaceProxyWithTargetGenerator.ImplementedByTarget<IInterface>(new Class2());59            obj3.Method1();ImplementedByTarget
Using AI Code Generation
1using System;2using Telerik.JustMock;3using Telerik.JustMock.Core;4using Telerik.JustMock.Helpers;5{6int GetInt();7}8{9public int GetInt()10{11return 1;12}13}14{15public int GetInt()16{17return 2;18}19}20{21public int GetInt()22{23return 3;24}25}26{27public int GetInt()28{29return 4;30}31}32{33public int GetInt()34{35return 5;36}37}38{39public int GetInt()40{41return 6;42}43}44{45public int GetInt()46{47return 7;48}49}50{51public int GetInt()52{53return 8;54}55}56{57public int GetInt()58{59return 9;60}61}62{63public int GetInt()64{65return 10;66}67}68{69public int GetInt()70{71return 11;72}73}74{75public int GetInt()76{77return 12;78}79}80{81public int GetInt()82{83return 13;84}85}86{87public int GetInt()88{89return 14;90}91}92{93public int GetInt()94{95return 15;96}97}98{99public int GetInt()100{101return 16;102}103}104{105public int GetInt()106{107return 17;108}109}110{111public int GetInt()112{113return 18;114}115}116{117public int GetInt()118{119return 19;120}121}122{123public int GetInt()124{125return 20;126}127}128{129public int GetInt()130{131return 21;132}133}134{135public int GetInt()136{137return 22;138}139}140{141public int GetInt()142{143return 23;144}145}146{147public int GetInt()148{149return 24;150}151}152{153public int GetInt()154{155return 25;156}157}158{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!!
