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

Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.MetaType

Run JustMockLite automation tests on LambdaTest cloud grid

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

InterfaceProxyWithoutTargetGenerator.cs

Source: InterfaceProxyWithoutTargetGenerator.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Generators
16{
17	using System;
18	using System.Collections.Generic;
19    using Telerik.JustMock.Core.Castle.Core.Logging;
20    using Telerik.JustMock.Core.Castle.DynamicProxy.Contributors;
21	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
22	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
23	using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;
25
26	internal class InterfaceProxyWithoutTargetGenerator : InterfaceProxyWithTargetGenerator
27	{
28		public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type @interface) : base(scope, @interface)
29		{
30		}
31
32		protected override string GeneratorType
33		{
34			get { return ProxyTypeConstants.InterfaceWithoutTarget; }
35		}
36
37		protected override ITypeContributor AddMappingForTargetType(
38			IDictionary<Type, ITypeContributor> interfaceTypeImplementerMapping, Type proxyTargetType,
39			ICollection<Type> targetInterfaces, ICollection<Type> additionalInterfaces, INamingScope namingScope)
40		{
41			var contributor = new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance)
42			{ Logger = this.Logger };
43			foreach (var @interface in targetType.GetAllInterfaces())
44			{
45				contributor.AddInterfaceToProxy(@interface);
46				AddMappingNoCheck(@interface, contributor, interfaceTypeImplementerMapping);
47			}
48			return contributor;
49		}
50
51		protected override Type GenerateType(string typeName, Type proxyTargetType, Type[] interfaces,
52		                                     INamingScope namingScope)
53		{
54			IEnumerable<ITypeContributor> contributors;
55			var allInterfaces = GetTypeImplementerMapping(interfaces, targetType, out contributors, namingScope);
56			var model = new MetaType();
57			// collect elements
58			foreach (var contributor in contributors)
59			{
60				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);
61			}
62
63			ProxyGenerationOptions.Hook.MethodsInspected();
64
65			ClassEmitter emitter;
66			FieldReference interceptorsField;
67			var baseType = Init(typeName, out emitter, proxyTargetType, out interceptorsField, allInterfaces);
68
69			// Constructor
70
71			var cctor = GenerateStaticConstructor(emitter);
72			var mixinFieldsList = new List<FieldReference>();
73
74			foreach (var contributor in contributors)
75			{
76				contributor.Generate(emitter, ProxyGenerationOptions);
77
78				// TODO: redo it
79				if (contributor is MixinContributor)
80				{
81					mixinFieldsList.AddRange((contributor as MixinContributor).Fields);
82				}
83			}
84
85			var ctorArguments = new List<FieldReference>(mixinFieldsList) { interceptorsField, targetField };
86			var selector = emitter.GetField("__selector");
87			if (selector != null)
88			{
89				ctorArguments.Add(selector);
90			}
91
92			GenerateConstructors(emitter, baseType, ctorArguments.ToArray());
93
94			// Complete type initializer code body
95			CompleteInitCacheMethod(cctor.CodeBuilder);
96
97			// Crosses fingers and build type
98			var generatedType = emitter.BuildType();
99
100			InitializeStaticFields(generatedType);
101			return generatedType;
102		}
103	}
104}
Full Screen

ITypeContributor.cs

Source: ITypeContributor.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
16{
17	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
18	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
19
20	/// <summary>
21	///   Interface describing elements composing generated type
22	/// </summary>
23	internal interface ITypeContributor
24	{
25		void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model);
26
27		void Generate(ClassEmitter @class, ProxyGenerationOptions options);
28	}
29}
Full Screen

DelegateProxyGenerator.cs

Source: DelegateProxyGenerator.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Generators
16{
17	using System;
18	using System.Collections.Generic;
19	using System.Reflection;
20#if FEATURE_SERIALIZATION
21	using System.Xml.Serialization;
22#endif
23
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Contributors;
25	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
26	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
27	using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;
28
29	internal class DelegateProxyGenerator : BaseProxyGenerator
30	{
31		public DelegateProxyGenerator(ModuleScope scope, Type delegateType) : base(scope, delegateType)
32		{
33			ProxyGenerationOptions = new ProxyGenerationOptions(new DelegateProxyGenerationHook());
34			ProxyGenerationOptions.Initialize();
35		}
36
37		public Type GetProxyType()
38		{
39			var cacheKey = new CacheKey(targetType, null, null);
40			return ObtainProxyType(cacheKey, GenerateType);
41		}
42
43		protected virtual IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContributor> contributors,
44		                                                              INamingScope namingScope)
45		{
46			var methodsToSkip = new List<MethodInfo>();
47			var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, Type.EmptyTypes,
48			                                                      ProxyTypeConstants.ClassWithTarget);
49			var proxyTarget = new DelegateProxyTargetContributor(targetType, namingScope) { Logger = Logger };
50			IDictionary<Type, ITypeContributor> typeImplementerMapping = new Dictionary<Type, ITypeContributor>();
51
52			// Order of interface precedence:
53			// 1. first target, target is not an interface so we do nothing
54			// 2. then mixins - we support none so we do nothing
55			// 3. then additional interfaces - we support none so we do nothing
56			// 4. plus special interfaces
57#if FEATURE_SERIALIZATION
58			if (targetType.IsSerializable)
59			{
60				AddMappingForISerializable(typeImplementerMapping, proxyInstance);
61			}
62#endif
63			AddMappingNoCheck(typeof(IProxyTargetAccessor), proxyInstance, typeImplementerMapping);
64
65			contributors = new List<ITypeContributor>
66			{
67				proxyTarget,
68				proxyInstance
69			};
70			return typeImplementerMapping.Keys;
71		}
72
73		private FieldReference CreateTargetField(ClassEmitter emitter)
74		{
75			var targetField = emitter.CreateField("__target", targetType);
76#if FEATURE_SERIALIZATION
77			emitter.DefineCustomAttributeFor<XmlIgnoreAttribute>(targetField);
78#endif
79			return targetField;
80		}
81
82		private Type GenerateType(string name, INamingScope namingScope)
83		{
84			IEnumerable<ITypeContributor> contributors;
85			var implementedInterfaces = GetTypeImplementerMapping(out contributors, namingScope);
86
87			var model = new MetaType();
88			// Collect methods
89			foreach (var contributor in contributors)
90			{
91				contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model);
92			}
93			ProxyGenerationOptions.Hook.MethodsInspected();
94
95			var emitter = BuildClassEmitter(name, typeof(object), implementedInterfaces);
96
97			CreateFields(emitter);
98			CreateTypeAttributes(emitter);
99
100			// Constructor
101			var cctor = GenerateStaticConstructor(emitter);
102
103			var targetField = CreateTargetField(emitter);
104			var constructorArguments = new List<FieldReference> { targetField };
105
106			foreach (var contributor in contributors)
107			{
108				contributor.Generate(emitter, ProxyGenerationOptions);
109			}
110
111			// constructor arguments
112			var interceptorsField = emitter.GetField("__interceptors");
113			constructorArguments.Add(interceptorsField);
114			var selector = emitter.GetField("__selector");
115			if (selector != null)
116			{
117				constructorArguments.Add(selector);
118			}
119
120			GenerateConstructor(emitter, null, constructorArguments.ToArray());
121			GenerateParameterlessConstructor(emitter, targetType, interceptorsField);
122
123			// Complete type initializer code body
124			CompleteInitCacheMethod(cctor.CodeBuilder);
125
126			// Crosses fingers and build type
127
128			var proxyType = emitter.BuildType();
129			InitializeStaticFields(proxyType);
130			return proxyType;
131		}
132	}
133}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in MetaType

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)