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

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

Run JustMockLite automation tests on LambdaTest cloud grid

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

MetaEvent.cs

Source: MetaEvent.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.Reflection;
19
20	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
21
22	internal class MetaEvent : MetaTypeElement, IEquatable<MetaEvent>
23	{
24		private readonly MetaMethod adder;
25		private readonly MetaMethod remover;
26		private readonly Type type;
27		private EventEmitter emitter;
28		private string name;
29
30		/// <summary>
31		///   Initializes a new instance of the <see cref = "MetaEvent" /> class.
32		/// </summary>
33		/// <param name = "name">The name.</param>
34		/// <param name = "declaringType">Type declaring the original event being overridden, or null.</param>
35		/// <param name = "eventDelegateType"></param>
36		/// <param name = "adder">The add method.</param>
37		/// <param name = "remover">The remove method.</param>
38		/// <param name = "attributes">The attributes.</param>
39		public MetaEvent(string name, Type declaringType, Type eventDelegateType, MetaMethod adder, MetaMethod remover,
40		                 EventAttributes attributes)
41			: base(declaringType)
42		{
43			if (adder == null)
44			{
45				throw new ArgumentNullException("adder");
46			}
47			if (remover == null)
48			{
49				throw new ArgumentNullException("remover");
50			}
51			this.name = name;
52			type = eventDelegateType;
53			this.adder = adder;
54			this.remover = remover;
55			Attributes = attributes;
56		}
57
58		public MetaMethod Adder
59		{
60			get { return adder; }
61		}
62
63		public EventAttributes Attributes { get; private set; }
64
65		public EventEmitter Emitter
66		{
67			get
68			{
69				if (emitter != null)
70				{
71					return emitter;
72				}
73
74				throw new InvalidOperationException(
75					"Emitter is not initialized. You have to initialize it first using 'BuildEventEmitter' method");
76			}
77		}
78
79		public MetaMethod Remover
80		{
81			get { return remover; }
82		}
83
84		public void BuildEventEmitter(ClassEmitter classEmitter)
85		{
86			if (emitter != null)
87			{
88				throw new InvalidOperationException();
89			}
90			emitter = classEmitter.CreateEvent(name, Attributes, type);
91		}
92
93		public override bool Equals(object obj)
94		{
95			if (ReferenceEquals(null, obj))
96			{
97				return false;
98			}
99			if (ReferenceEquals(this, obj))
100			{
101				return true;
102			}
103			if (obj.GetType() != typeof(MetaEvent))
104			{
105				return false;
106			}
107			return Equals((MetaEvent)obj);
108		}
109
110		public override int GetHashCode()
111		{
112			unchecked
113			{
114				var result = (adder.Method != null ? adder.Method.GetHashCode() : 0);
115				result = (result*397) ^ (remover.Method != null ? remover.Method.GetHashCode() : 0);
116				result = (result*397) ^ Attributes.GetHashCode();
117				return result;
118			}
119		}
120
121		public bool Equals(MetaEvent other)
122		{
123			if (ReferenceEquals(null, other))
124			{
125				return false;
126			}
127
128			if (ReferenceEquals(this, other))
129			{
130				return true;
131			}
132
133			if (!type.Equals(other.type))
134			{
135				return false;
136			}
137
138			if (!StringComparer.OrdinalIgnoreCase.Equals(name, other.name))
139			{
140				return false;
141			}
142
143			return true;
144		}
145
146		internal override void SwitchToExplicitImplementation()
147		{
148			name = MetaTypeElementUtil.CreateNameForExplicitImplementation(sourceType, name);
149			adder.SwitchToExplicitImplementation();
150			remover.SwitchToExplicitImplementation();
151		}
152	}
153}
Full Screen

EventCollection.cs

Source: EventCollection.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.Emitters
16{
17	using System.Collections.ObjectModel;
18
19	internal class EventCollection : Collection<EventEmitter>
20	{
21	}
22}
Full Screen

AbstractTypeEmitter.cs

Source: AbstractTypeEmitter.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.Emitters
16{
17	using System;
18	using System.Collections.Generic;
19	using System.Diagnostics;
20	using System.Reflection;
21	using System.Reflection.Emit;
22
23	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
24	using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;
25
26	internal abstract class AbstractTypeEmitter
27	{
28		private const MethodAttributes defaultAttributes =
29			MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public;
30
31		private readonly ConstructorCollection constructors;
32		private readonly EventCollection events;
33
34		private readonly IDictionary<string, FieldReference> fields =
35			new Dictionary<string, FieldReference>(StringComparer.OrdinalIgnoreCase);
36
37		private readonly MethodCollection methods;
38
39		private readonly Dictionary<String, GenericTypeParameterBuilder> name2GenericType;
40		private readonly NestedClassCollection nested;
41		private readonly PropertiesCollection properties;
42		private readonly TypeBuilder typebuilder;
43
44		private GenericTypeParameterBuilder[] genericTypeParams;
45
46		protected AbstractTypeEmitter(TypeBuilder typeBuilder)
47		{
48			typebuilder = typeBuilder;
49			nested = new NestedClassCollection();
50			methods = new MethodCollection();
51			constructors = new ConstructorCollection();
52			properties = new PropertiesCollection();
53			events = new EventCollection();
54			name2GenericType = new Dictionary<String, GenericTypeParameterBuilder>();
55		}
56
57		public Type BaseType
58		{
59			get
60			{
61				if (TypeBuilder.IsInterface)
62				{
63					throw new InvalidOperationException("This emitter represents an interface; interfaces have no base types.");
64				}
65				return TypeBuilder.BaseType;
66			}
67		}
68
69		public TypeConstructorEmitter ClassConstructor { get; private set; }
70
71		public ConstructorCollection Constructors
72		{
73			get { return constructors; }
74		}
75
76		public GenericTypeParameterBuilder[] GenericTypeParams
77		{
78			get { return genericTypeParams; }
79		}
80
81		public NestedClassCollection Nested
82		{
83			get { return nested; }
84		}
85
86		public TypeBuilder TypeBuilder
87		{
88			get { return typebuilder; }
89		}
90
91		public void AddCustomAttributes(ProxyGenerationOptions proxyGenerationOptions)
92		{
93            foreach (var attribute in proxyGenerationOptions.AdditionalAttributes)
94			{
95				typebuilder.SetCustomAttribute(attribute.Builder);
96			}
97		}
98
99		public virtual Type BuildType()
100		{
101			EnsureBuildersAreInAValidState();
102
103			var type = CreateType(typebuilder);
104
105			foreach (var builder in nested)
106			{
107				builder.BuildType();
108			}
109
110			return type;
111		}
112
113		public void CopyGenericParametersFromMethod(MethodInfo methodToCopyGenericsFrom)
114		{
115			// big sanity check
116			if (genericTypeParams != null)
117			{
118				throw new ProxyGenerationException("CopyGenericParametersFromMethod: cannot invoke me twice");
119			}
120
121			SetGenericTypeParameters(GenericUtil.CopyGenericArguments(methodToCopyGenericsFrom, typebuilder, name2GenericType));
122		}
123
124		public ConstructorEmitter CreateConstructor(params ArgumentReference[] arguments)
125		{
126			if (TypeBuilder.IsInterface)
127			{
128				throw new InvalidOperationException("Interfaces cannot have constructors.");
129			}
130
131			var member = new ConstructorEmitter(this, arguments);
132			constructors.Add(member);
133			return member;
134		}
135
136		public void CreateDefaultConstructor()
137		{
138			if (TypeBuilder.IsInterface)
139			{
140				throw new InvalidOperationException("Interfaces cannot have constructors.");
141			}
142
143			constructors.Add(new ConstructorEmitter(this));
144		}
145
146		public EventEmitter CreateEvent(string name, EventAttributes atts, Type type)
147		{
148			var eventEmitter = new EventEmitter(this, name, atts, type);
149			events.Add(eventEmitter);
150			return eventEmitter;
151		}
152
153		public FieldReference CreateField(string name, Type fieldType)
154		{
155			return CreateField(name, fieldType, true);
156		}
157
158		public FieldReference CreateField(string name, Type fieldType, bool serializable)
159		{
160			var atts = FieldAttributes.Private;
161
162			if (!serializable)
163			{
164				atts |= FieldAttributes.NotSerialized;
165			}
166
167			return CreateField(name, fieldType, atts);
168		}
169
170		public FieldReference CreateField(string name, Type fieldType, FieldAttributes atts)
171		{
172			var fieldBuilder = typebuilder.DefineField(name, fieldType, atts);
173			var reference = new FieldReference(fieldBuilder);
174			fields[name] = reference;
175			return reference;
176		}
177
178		public MethodEmitter CreateMethod(string name, MethodAttributes attrs, Type returnType, params Type[] argumentTypes)
179		{
180			var member = new MethodEmitter(this, name, attrs, returnType, argumentTypes ?? Type.EmptyTypes);
181			methods.Add(member);
182			return member;
183		}
184
185		public MethodEmitter CreateMethod(string name, Type returnType, params Type[] parameterTypes)
186		{
187			return CreateMethod(name, defaultAttributes, returnType, parameterTypes);
188		}
189
190		public MethodEmitter CreateMethod(string name, MethodInfo methodToUseAsATemplate)
191		{
192			return CreateMethod(name, defaultAttributes, methodToUseAsATemplate);
193		}
194
195		public MethodEmitter CreateMethod(string name, MethodAttributes attributes, MethodInfo methodToUseAsATemplate)
196		{
197			var method = new MethodEmitter(this, name, attributes, methodToUseAsATemplate);
198			methods.Add(method);
199			return method;
200		}
201
202		public PropertyEmitter CreateProperty(string name, PropertyAttributes attributes, Type propertyType, Type[] arguments)
203		{
204			var propEmitter = new PropertyEmitter(this, name, attributes, propertyType, arguments);
205			properties.Add(propEmitter);
206			return propEmitter;
207		}
208
209		public FieldReference CreateStaticField(string name, Type fieldType)
210		{
211			return CreateStaticField(name, fieldType, FieldAttributes.Private);
212		}
213
214		public FieldReference CreateStaticField(string name, Type fieldType, FieldAttributes atts)
215		{
216			atts |= FieldAttributes.Static;
217			return CreateField(name, fieldType, atts);
218		}
219
220		public ConstructorEmitter CreateTypeConstructor()
221		{
222			var member = new TypeConstructorEmitter(this);
223			constructors.Add(member);
224			ClassConstructor = member;
225			return member;
226		}
227
228		public void DefineCustomAttribute(CustomAttributeBuilder attribute)
229		{
230			typebuilder.SetCustomAttribute(attribute);
231		}
232
233		public void DefineCustomAttribute<TAttribute>(object[] constructorArguments) where TAttribute : Attribute
234		{
235			var customAttributeInfo = AttributeUtil.CreateInfo(typeof(TAttribute), constructorArguments);
236			typebuilder.SetCustomAttribute(customAttributeInfo.Builder);
237		}
238
239		public void DefineCustomAttribute<TAttribute>() where TAttribute : Attribute, new()
240		{
241			var customAttributeInfo = AttributeUtil.CreateInfo<TAttribute>();
242			typebuilder.SetCustomAttribute(customAttributeInfo.Builder);
243		}
244
245		public void DefineCustomAttributeFor<TAttribute>(FieldReference field) where TAttribute : Attribute, new()
246		{
247			var customAttributeInfo = AttributeUtil.CreateInfo<TAttribute>();
248			var fieldbuilder = field.Fieldbuilder;
249			if (fieldbuilder == null)
250			{
251				throw new ArgumentException(
252					"Invalid field reference.This reference does not point to field on type being generated", "field");
253			}
254			fieldbuilder.SetCustomAttribute(customAttributeInfo.Builder);
255		}
256
257		public IEnumerable<FieldReference> GetAllFields()
258		{
259			return fields.Values;
260		}
261
262		public FieldReference GetField(string name)
263		{
264			if (string.IsNullOrEmpty(name))
265			{
266				return null;
267			}
268
269			FieldReference value;
270			fields.TryGetValue(name, out value);
271			return value;
272		}
273
274		public Type GetGenericArgument(String genericArgumentName)
275		{
276			if (name2GenericType.ContainsKey(genericArgumentName))
277				return name2GenericType[genericArgumentName].AsType();
278
279			return null;
280		}
281
282		public Type[] GetGenericArgumentsFor(Type genericType)
283		{
284			var types = new List<Type>();
285
286			foreach (var genType in genericType.GetGenericArguments())
287			{
288				if (genType.GetTypeInfo().IsGenericParameter)
289				{
290					types.Add(name2GenericType[genType.Name].AsType());
291				}
292				else
293				{
294					types.Add(genType);
295				}
296			}
297
298			return types.ToArray();
299		}
300
301		public Type[] GetGenericArgumentsFor(MethodInfo genericMethod)
302		{
303			var types = new List<Type>();
304			foreach (var genType in genericMethod.GetGenericArguments())
305			{
306				types.Add(name2GenericType[genType.Name].AsType());
307			}
308
309			return types.ToArray();
310		}
311
312		public void SetGenericTypeParameters(GenericTypeParameterBuilder[] genericTypeParameterBuilders)
313		{
314			genericTypeParams = genericTypeParameterBuilders;
315		}
316
317		protected Type CreateType(TypeBuilder type)
318		{
319			try
320			{
321#if FEATURE_LEGACY_REFLECTION_API
322				return type.CreateType();
323#else
324				return type.CreateTypeInfo().AsType();
325#endif
326			}
327			catch (BadImageFormatException ex)
328			{
329				if (Debugger.IsAttached == false)
330				{
331					throw;
332				}
333
334				if (ex.Message.Contains(@"HRESULT: 0x8007000B") == false)
335				{
336					throw;
337				}
338
339				if (type.IsGenericTypeDefinition == false)
340				{
341					throw;
342				}
343
344				var message =
345					"This is a DynamicProxy2 error: It looks like you encountered a bug in Visual Studio debugger, " +
346					"which causes this exception when proxying types with generic methods having constraints on their generic arguments." +
347					"This code will work just fine without the debugger attached. " +
348					"If you wish to use debugger you may have to switch to Visual Studio 2010 where this bug was fixed.";
349				var exception = new ProxyGenerationException(message);
350				exception.Data.Add("ProxyType", type.ToString());
351				throw exception;
352			}
353		}
354
355		protected virtual void EnsureBuildersAreInAValidState()
356		{
357			if (!typebuilder.IsInterface && constructors.Count == 0)
358			{
359				CreateDefaultConstructor();
360			}
361
362			foreach (IMemberEmitter builder in properties)
363			{
364				builder.EnsureValidCodeBlock();
365				builder.Generate();
366			}
367			foreach (IMemberEmitter builder in events)
368			{
369				builder.EnsureValidCodeBlock();
370				builder.Generate();
371			}
372			foreach (IMemberEmitter builder in constructors)
373			{
374				builder.EnsureValidCodeBlock();
375				builder.Generate();
376			}
377			foreach (IMemberEmitter builder in methods)
378			{
379				builder.EnsureValidCodeBlock();
380				builder.Generate();
381			}
382		}
383	}
384}
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

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)