How to use ConvertExpression class of Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST package

Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST.ConvertExpression

Run JustMockLite automation tests on LambdaTest cloud grid

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

InvocationWithDelegateContributor.cs

Source: InvocationWithDelegateContributor.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 System;
18	using System.Diagnostics;
19	using System.Reflection;
20
21	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
22	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
23	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;
25
26	internal class InvocationWithDelegateContributor : IInvocationCreationContributor
27	{
28		private readonly Type delegateType;
29		private readonly MetaMethod method;
30		private readonly INamingScope namingScope;
31		private readonly Type targetType;
32
33		public InvocationWithDelegateContributor(Type delegateType, Type targetType, MetaMethod method,
34		                                         INamingScope namingScope)
35		{
36			Debug.Assert(delegateType.GetTypeInfo().IsGenericType == false, "delegateType.IsGenericType == false");
37			this.delegateType = delegateType;
38			this.targetType = targetType;
39			this.method = method;
40			this.namingScope = namingScope;
41		}
42
43		public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
44		{
45			var arguments = GetArguments(baseCtorArguments);
46			var constructor = invocation.CreateConstructor(arguments);
47
48			var delegateField = invocation.CreateField("delegate", delegateType);
49			constructor.CodeBuilder.AddStatement(new AssignStatement(delegateField, new ReferenceExpression(arguments[0])));
50			return constructor;
51		}
52
53		public MethodInfo GetCallbackMethod()
54		{
55			return delegateType.GetMethod("Invoke");
56		}
57
58		public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
59		                                                              Reference targetField,
60		                                                              MethodEmitter invokeMethodOnTarget)
61		{
62			var allArgs = GetAllArgs(args, targetField);
63			var @delegate = (Reference)invocation.GetField("delegate");
64
65			return new MethodInvocationExpression(@delegate, GetCallbackMethod(), allArgs);
66		}
67
68		public Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy)
69		{
70			var allArguments = new Expression[arguments.Length + 1];
71			allArguments[0] = new ReferenceExpression(BuildDelegateToken(proxy));
72			Array.Copy(arguments, 0, allArguments, 1, arguments.Length);
73			return allArguments;
74		}
75
76		private FieldReference BuildDelegateToken(ClassEmitter proxy)
77		{
78			var callback = proxy.CreateStaticField(namingScope.GetUniqueName("callback_" + method.Method.Name), delegateType);
79			var createDelegate = new MethodInvocationExpression(
80				null,
81				DelegateMethods.CreateDelegate,
82				new TypeTokenExpression(delegateType),
83				NullExpression.Instance,
84				new MethodTokenExpression(method.MethodOnTarget));
85			var bindDelegate = new AssignStatement(callback, new ConvertExpression(delegateType, createDelegate));
86
87			proxy.ClassConstructor.CodeBuilder.AddStatement(bindDelegate);
88			return callback;
89		}
90
91		private Expression[] GetAllArgs(Expression[] args, Reference targetField)
92		{
93			var allArgs = new Expression[args.Length + 1];
94			args.CopyTo(allArgs, 1);
95			allArgs[0] = new ConvertExpression(targetType, targetField.ToExpression());
96			return allArgs;
97		}
98
99		private ArgumentReference[] GetArguments(ArgumentReference[] baseCtorArguments)
100		{
101			var arguments = new ArgumentReference[baseCtorArguments.Length + 1];
102			arguments[0] = new ArgumentReference(delegateType);
103			baseCtorArguments.CopyTo(arguments, 1);
104			return arguments;
105		}
106	}
107}
Full Screen

InvocationWithGenericDelegateContributor.cs

Source: InvocationWithGenericDelegateContributor.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 System;
18	using System.Diagnostics;
19	using System.Linq;
20	using System.Reflection;
21
22	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
23	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
25	using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;
26	using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;
27
28	internal class InvocationWithGenericDelegateContributor : IInvocationCreationContributor
29	{
30		private readonly Type delegateType;
31		private readonly MetaMethod method;
32		private readonly Reference targetReference;
33
34		public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, Reference targetReference)
35		{
36			Debug.Assert(delegateType.GetTypeInfo().IsGenericType, "delegateType.IsGenericType");
37			this.delegateType = delegateType;
38			this.method = method;
39			this.targetReference = targetReference;
40		}
41
42		public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
43		{
44			return invocation.CreateConstructor(baseCtorArguments);
45		}
46
47		public MethodInfo GetCallbackMethod()
48		{
49			return delegateType.GetMethod("Invoke");
50		}
51
52		public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
53		                                                              Reference targetField,
54		                                                              MethodEmitter invokeMethodOnTarget)
55		{
56			var @delegate = GetDelegate(invocation, invokeMethodOnTarget);
57			return new MethodInvocationExpression(@delegate, GetCallbackMethod(), args);
58		}
59
60		public Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy)
61		{
62			return arguments;
63		}
64
65		private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
66		{
67			var genericTypeParameters = invocation.GenericTypeParams.AsTypeArray();
68			var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters);
69			var localReference = invokeMethodOnTarget.CodeBuilder.DeclareLocal(closedDelegateType);
70			var closedMethodOnTarget = method.MethodOnTarget.MakeGenericMethod(genericTypeParameters);
71			var localTarget = new ReferenceExpression(targetReference);
72			invokeMethodOnTarget.CodeBuilder.AddStatement(
73				SetDelegate(localReference, localTarget, closedDelegateType, closedMethodOnTarget));
74			return localReference;
75		}
76
77		private AssignStatement SetDelegate(LocalReference localDelegate, ReferenceExpression localTarget,
78		                                    Type closedDelegateType, MethodInfo closedMethodOnTarget)
79		{
80			var delegateCreateDelegate = new MethodInvocationExpression(
81				null,
82				DelegateMethods.CreateDelegate,
83				new TypeTokenExpression(closedDelegateType),
84				localTarget,
85				new MethodTokenExpression(closedMethodOnTarget));
86			return new AssignStatement(localDelegate, new ConvertExpression(closedDelegateType, delegateCreateDelegate));
87		}
88	}
89}
Full Screen

ProxyInstanceContributor.cs

Source: ProxyInstanceContributor.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 System;
18	using System.Reflection;
19#if FEATURE_SERIALIZATION
20	using System.Runtime.Serialization;
21#endif
22
23	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
25	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.CodeBuilders;
26	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
27	using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;
28#if FEATURE_SERIALIZATION
29	using Telerik.JustMock.Core.Castle.DynamicProxy.Serialization;
30#endif
31    using Telerik.JustMock.Core.Castle.DynamicProxy.Tokens;
32
33	internal abstract class ProxyInstanceContributor : ITypeContributor
34	{
35		protected readonly Type targetType;
36		private readonly string proxyTypeId;
37		private readonly Type[] interfaces;
38
39		protected ProxyInstanceContributor(Type targetType, Type[] interfaces, string proxyTypeId)
40		{
41			this.targetType = targetType;
42			this.proxyTypeId = proxyTypeId;
43			this.interfaces = interfaces ?? Type.EmptyTypes;
44		}
45
46		protected abstract Reference GetTargetReference(ClassEmitter emitter);
47
48		private Expression GetTargetReferenceExpression(ClassEmitter emitter)
49		{
50			return GetTargetReference(emitter).ToExpression();
51		}
52
53		public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)
54		{
55			var interceptors = @class.GetField("__interceptors");
56#if FEATURE_SERIALIZATION
57			ImplementGetObjectData(@class);
58#endif
59			ImplementProxyTargetAccessor(@class, interceptors);
60			foreach (var attribute in targetType.GetTypeInfo().GetNonInheritableAttributes())
61			{
62				@class.DefineCustomAttribute(attribute.Builder);
63			}
64		}
65
66		protected void ImplementProxyTargetAccessor(ClassEmitter emitter, FieldReference interceptorsField)
67		{
68			var dynProxyGetTarget = emitter.CreateMethod("DynProxyGetTarget", typeof(object));
69
70			dynProxyGetTarget.CodeBuilder.AddStatement(
71				new ReturnStatement(new ConvertExpression(typeof(object), targetType, GetTargetReferenceExpression(emitter))));
72
73			var dynProxySetTarget = emitter.CreateMethod("DynProxySetTarget", typeof(void), typeof(object));
74
75			// we can only change the target of the interface proxy
76			var targetField = GetTargetReference(emitter) as FieldReference;
77			if (targetField != null)
78			{
79				dynProxySetTarget.CodeBuilder.AddStatement(
80					new AssignStatement(targetField,
81						new ConvertExpression(targetField.Fieldbuilder.FieldType, dynProxySetTarget.Arguments[0].ToExpression())));
82			}
83			else
84			{
85				dynProxySetTarget.CodeBuilder.AddStatement(
86					new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy."));
87			}
88
89			dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement());
90
91			var getInterceptors = emitter.CreateMethod("GetInterceptors", typeof(IInterceptor[]));
92
93			getInterceptors.CodeBuilder.AddStatement(
94				new ReturnStatement(interceptorsField));
95		}
96
97#if FEATURE_SERIALIZATION
98		protected void ImplementGetObjectData(ClassEmitter emitter)
99		{
100			var getObjectData = emitter.CreateMethod("GetObjectData", typeof(void),
101			                                         new[] { typeof(SerializationInfo), typeof(StreamingContext) });
102			var info = getObjectData.Arguments[0];
103
104			var typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type));
105
106			getObjectData.CodeBuilder.AddStatement(
107				new AssignStatement(
108					typeLocal,
109					new MethodInvocationExpression(
110						null,
111						TypeMethods.StaticGetType,
112						new ConstReference(typeof(ProxyObjectReference).AssemblyQualifiedName).ToExpression(),
113						new ConstReference(1).ToExpression(),
114						new ConstReference(0).ToExpression())));
115
116			getObjectData.CodeBuilder.AddStatement(
117				new ExpressionStatement(
118					new MethodInvocationExpression(
119						info,
120						SerializationInfoMethods.SetType,
121						typeLocal.ToExpression())));
122
123			foreach (var field in emitter.GetAllFields())
124			{
125				if (field.Reference.IsStatic)
126				{
127					continue;
128				}
129				if (field.Reference.IsNotSerialized)
130				{
131					continue;
132				}
133				AddAddValueInvocation(info, getObjectData, field);
134			}
135
136			var interfacesLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(string[]));
137
138			getObjectData.CodeBuilder.AddStatement(
139				new AssignStatement(
140					interfacesLocal,
141					new NewArrayExpression(interfaces.Length, typeof(string))));
142
143			for (var i = 0; i < interfaces.Length; i++)
144			{
145				getObjectData.CodeBuilder.AddStatement(
146					new AssignArrayStatement(
147						interfacesLocal,
148						i,
149						new ConstReference(interfaces[i].AssemblyQualifiedName).ToExpression()));
150			}
151
152			getObjectData.CodeBuilder.AddStatement(
153				new ExpressionStatement(
154					new MethodInvocationExpression(
155						info,
156						SerializationInfoMethods.AddValue_Object,
157						new ConstReference("__interfaces").ToExpression(),
158						interfacesLocal.ToExpression())));
159
160			getObjectData.CodeBuilder.AddStatement(
161				new ExpressionStatement(
162					new MethodInvocationExpression(
163						info,
164						SerializationInfoMethods.AddValue_Object,
165						new ConstReference("__baseType").ToExpression(),
166						new ConstReference(emitter.BaseType.AssemblyQualifiedName).ToExpression())));
167
168			getObjectData.CodeBuilder.AddStatement(
169				new ExpressionStatement(
170					new MethodInvocationExpression(
171						info,
172						SerializationInfoMethods.AddValue_Object,
173						new ConstReference("__proxyGenerationOptions").ToExpression(),
174						emitter.GetField("proxyGenerationOptions").ToExpression())));
175
176			getObjectData.CodeBuilder.AddStatement(
177				new ExpressionStatement(
178					new MethodInvocationExpression(info,
179					                               SerializationInfoMethods.AddValue_Object,
180					                               new ConstReference("__proxyTypeId").ToExpression(),
181					                               new ConstReference(proxyTypeId).ToExpression())));
182
183			CustomizeGetObjectData(getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter);
184
185			getObjectData.CodeBuilder.AddStatement(new ReturnStatement());
186		}
187
188		protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,
189		                                             FieldReference field)
190		{
191			getObjectData.CodeBuilder.AddStatement(
192				new ExpressionStatement(
193					new MethodInvocationExpression(
194						serializationInfo,
195						SerializationInfoMethods.AddValue_Object,
196						new ConstReference(field.Reference.Name).ToExpression(),
197						field.ToExpression())));
198			return;
199		}
200
201		protected abstract void CustomizeGetObjectData(AbstractCodeBuilder builder, ArgumentReference serializationInfo,
202		                                               ArgumentReference streamingContext, ClassEmitter emitter);
203#endif
204
205		public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)
206		{
207		}
208	}
209}
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 ConvertExpression

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)