Inheritance: AbstractTypeEmitter
		public Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy)
		{
			var allArguments = new Expression[arguments.Length + 1];
			allArguments[0] = new ReferenceExpression(BuildDelegateToken(proxy));
			Array.Copy(arguments, 0, allArguments, 1, arguments.Length);
			return allArguments;
		}
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
		                                                      ProxyGenerationOptions options,
		                                                      OverrideMethodDelegate overrideMethod)
		{
			if (methodsToSkip.Contains(method.Method))
			{
				return null;
			}

			if (!method.Proxyable)
			{
				return new MinimialisticMethodGenerator(method,
				                                        overrideMethod);
			}

			if (IsDirectlyAccessible(method) == false)
			{
				return IndirectlyCalledMethodGenerator(method, @class, options, overrideMethod);
			}

			var invocation = GetInvocationType(method, @class, options);

			return new MethodWithInvocationGenerator(method,
			                                         @class.GetField("__interceptors"),
			                                         invocation,
			                                         (c, m) => c.GetField("__target").ToExpression(),
			                                         overrideMethod,
			                                         null);
		}
		public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)
		{
			foreach (var method in methods)
			{
				if (!method.Standalone)
				{
					continue;
				}

				ImplementMethod(method,
				                @class,
				                options,
				                @class.CreateMethod);
			}

			foreach (var property in properties)
			{
				ImplementProperty(@class, property, options);
			}

			foreach (var @event in events)
			{
				ImplementEvent(@class, @event, options);
			}
		}
示例#4
0
		public void ForceUnsignedTrueWithSignedTypes()
		{
			ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes,
			                                        TypeAttributes.Public, true);
			Type t = emitter.BuildType();
			Assert.IsFalse(StrongNameUtil.IsAssemblySigned(t.GetTypeInfo().Assembly));
		}
		/// <summary>
		/// Initializes a new instance of the AdapterBuilderContext class.
		/// </summary>
		/// <param name="originalObject"></param>
		/// <param name="classEmitter"></param>
		/// <param name="adaptedObjectRef"></param>
		public AdapterBuilderStageContext(Type originalObject, ClassEmitter classEmitter, FieldReference adaptedObjectRef, MethodInfo originalMethod)
		{
			OriginalObject = originalObject;
			ClassEmitter = classEmitter;
			AdaptedObjectRef = adaptedObjectRef;
			OriginalMethod = originalMethod;
		}
		private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
		{
			var scope = @class.ModuleScope;

			Type[] invocationInterfaces;
			if (canChangeTarget)
			{
				invocationInterfaces = new[] { typeof(IInvocation), typeof(IChangeProxyTarget) };
			}
			else
			{
				invocationInterfaces = new[] { typeof(IInvocation) };
			}

			var key = new CacheKey(method.Method, CompositionInvocationTypeGenerator.BaseType, invocationInterfaces, null);

			// no locking required as we're already within a lock

			var invocation = scope.GetFromCache(key);
			if (invocation != null)
			{
				return invocation;
			}

			invocation = new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
			                                                    method,
			                                                    method.Method,
			                                                    canChangeTarget,
			                                                    null)
				.Generate(@class, options, namingScope).BuildType();

			scope.RegisterInCache(key, invocation);

			return invocation;
		}
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
		                                                      ProxyGenerationOptions options,
		                                                      OverrideMethodDelegate overrideMethod)
		{
			if (!method.Proxyable)
			{
				return new ForwardingMethodGenerator(method,
				                                     overrideMethod,
				                                     (c, m) => c.GetField("__target"));
			}

			var targetMethod = options.ProxyEffectiveType == null ? method.Method : InvocationHelper.GetMethodOnType(proxyTargetType, method.Method);

			if (targetMethod == null)
			{
				return new MinimialisticMethodGenerator(method, overrideMethod);
			}

			var invocation = GetInvocationType(method, targetMethod, @class, options);

			return new MethodWithInvocationGenerator(method,
			                                         @class.GetField("__interceptors"),
			                                         invocation,
			                                         (c, m) => c.GetField("__target").ToExpression(),
			                                         overrideMethod,
			                                         null);
		}
		private static TypeBuilder CreateTypeBuilder (ClassEmitter maintype, string name, Type baseType, Type[] interfaces)
		{
			return maintype.TypeBuilder.DefineNestedType (
				name,
				TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class,
				baseType, interfaces);
		}
		public void AutomaticDefaultConstructorGenerationWithClosedGenericType()
		{
			ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>),
			                                        Type.EmptyTypes);
			Type t = emitter.BuildType();
			Activator.CreateInstance(t);
		}
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
		                                                      ProxyGenerationOptions options,
		                                                      OverrideMethodDelegate overrideMethod)
		{
			if (methodsToSkip.Contains(method.Method))
				return null;

			if (!method.Proxyable)
			{
				return new MinimialisticMethodGenerator(method,
				                                        overrideMethod);
			}

			if (ExplicitlyImplementedInterfaceMethod(method))
			{
#if SILVERLIGHT
				return null;
#else
				return ExplicitlyImplementedInterfaceMethodGenerator(method, @class, options, overrideMethod);
#endif
			}

			var invocation = GetInvocationType(method, @class, options);

			return new MethodWithInvocationGenerator(method,
			                                         @class.GetField("__interceptors"),
			                                         invocation,
			                                         (c, m) => new TypeTokenExpression(targetType),
			                                         overrideMethod,
			                                         null);
		}
		// 重载 InterfaceProxyWithTargetInterfaceContributor 中的方法,以指定使用扩展的 InvocationType 生成方法执行类
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
		{
			if (!method.Proxyable)
			{
				return new MinimialisticMethodGenerator(method, overrideMethod);
			}
			return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), this.GetInvocationType(method, @class, options), this.getTargetExpression, overrideMethod, null);
		}
		// 重载 InterfaceProxyWithTargetInterfaceContributor 中的方法,以指定使用扩展的 InvocationType 生成方法执行类
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
		{
			if (!method.Proxyable)
			{
				return new ForwardingMethodGenerator(method, overrideMethod, (c, m) => c.GetField("__target"));
			}
			return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), this.GetInvocationType(method, @class, options), (c, m) => c.GetField("__target").ToExpression(), overrideMethod, null);
		}
示例#13
0
		public void ForceUnsignedFalseWithSignedTypes()
		{
			const bool shouldBeSigned = true;
			ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes,
			                                        TypeAttributes.Public, false);
			Type t = emitter.BuildType();
			Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.GetTypeInfo().Assembly));
		}
示例#14
0
		private FieldReference CreateTargetField(ClassEmitter emitter)
		{
			var targetField = emitter.CreateField("__target", targetType);
#if FEATURE_SERIALIZATION
			emitter.DefineCustomAttributeFor<XmlIgnoreAttribute>(targetField);
#endif
			return targetField;
		}
示例#15
0
		private FieldReference CreateTargetField(ClassEmitter emitter)
		{
			var targetField = emitter.CreateField("__target", targetType);
#if !SILVERLIGHT
			emitter.DefineCustomAttributeFor<XmlIgnoreAttribute>(targetField);
#endif
			return targetField;
		}
		public void StaticMethodArguments ()
		{
			ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>), Type.EmptyTypes);
			MethodEmitter methodEmitter = emitter.CreateMethod ("StaticMethod", MethodAttributes.Public | MethodAttributes.Static,
					typeof (string), typeof (string));
			methodEmitter.CodeBuilder.AddStatement (new ReturnStatement (methodEmitter.Arguments[0]));
			Type t = emitter.BuildType ();
			Assert.AreEqual ("five", t.GetMethod ("StaticMethod").Invoke (null, new object[] { "five" }));
		}
        protected override void CreateInvocationForMethod(ClassEmitter emitter, MethodInfo method, Type proxyTargetType)
        {
            method2methodOnTarget[method] = method;

            method2Invocation[method] = BuildInvocationNestedType(emitter, targetType,
                                                                  proxyTargetType,
                                                                  method, null,
                                                                  ConstructorVersion.WithTargetMethod);
        }
		protected void SetGenerationOptions (ProxyGenerationOptions options, ClassEmitter emitter)
		{
			if (proxyGenerationOptions != null)
			{
				throw new InvalidOperationException ("ProxyGenerationOptions can only be set once.");
			}
			proxyGenerationOptions = options;
			proxyGenerationOptionsField = emitter.CreateStaticField ("proxyGenerationOptions", typeof (ProxyGenerationOptions));
		}
		public void InstanceMethodArguments ()
		{
			ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "Foo", typeof (List<object>), Type.EmptyTypes);
			MethodEmitter methodEmitter = emitter.CreateMethod ("InstanceMethod", MethodAttributes.Public,
					typeof (string), typeof (string));
			methodEmitter.CodeBuilder.AddStatement (new ReturnStatement (methodEmitter.Arguments[0]));
			Type t = emitter.BuildType ();
			object instance = Activator.CreateInstance (t);
			Assert.AreEqual ("six", t.GetMethod ("InstanceMethod").Invoke (instance, new object[] { "six" }));
		}
 /// <summary>
 /// Generates the class defined by the provided class emitter.
 /// </summary>
 /// <param name="class">
 /// The <see cref="Castle.DynamicProxy.Generators.Emitters.ClassEmitter"/>
 /// being used to build the target type.
 /// </param>
 /// <param name="options">The options to use during proxy generation.</param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown if <paramref name="class" /> is <see langword="null" />.
 /// </exception>
 /// <remarks>
 /// <para>
 /// This overridden version of the method does everything that the base
 /// <see cref="Castle.DynamicProxy.Contributors.ProxyInstanceContributor.Generate"/>
 /// method does but it skips the part where it checks for non-inherited
 /// attributes and copies them over from the proxy target.
 /// </para>
 /// </remarks>
 public override void Generate(ClassEmitter @class, ProxyGenerationOptions options)
 {
     if (@class == null)
     {
         throw new ArgumentNullException("class");
     }
     FieldReference field = @class.GetField("__interceptors");
     this.ImplementGetObjectData(@class);
     this.ImplementProxyTargetAccessor(@class, field);
 }
		protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
		                                                        ProxyGenerationOptions options, INamingScope namingScope)
		{
			var targetReference = getTargetReference(@class, MethodToOverride);

			emitter.CodeBuilder.AddStatement(
				new ExpressionStatement(
					new IfNullExpression(targetReference, IfNull(emitter.ReturnType), IfNotNull(targetReference))));
			return emitter;
		}
        protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
                                                              ProxyGenerationOptions options,
                                                              OverrideMethodDelegate overrideMethod)
        {
            if (!method.Proxyable)
            {
                return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTargetReference);
            }

            return base.GetMethodGenerator(method, @class, options, overrideMethod);
        }
		protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope)
		{
			var methodInterceptors = @class.CreateField(
				namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)),
				typeof(IInterceptor[]),
				false);
#if !SILVERLIGHT
			@class.DefineCustomAttributeFor<XmlIgnoreAttribute>(methodInterceptors);
#endif
			return methodInterceptors;
		}
		protected override void CreateInvocationForMethod(ClassEmitter emitter, MethodInfo method, Type proxyTargetType)
		{
			MethodInfo methodOnTarget = FindMethodOnTargetType(method, proxyTargetType, true);

			method2methodOnTarget[method] = methodOnTarget;

			method2Invocation[method] = BuildInvocationNestedType(emitter, proxyTargetType,
																														IsMixinMethod (method) ? method.DeclaringType : proxyTargetType,
																														method, methodOnTarget,
			                                                      ConstructorVersion.WithTargetMethod, true);
		}
示例#25
0
		public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)
		{
			var interceptors = @class.GetField("__interceptors");
#if FEATURE_SERIALIZATION
			ImplementGetObjectData(@class);
#endif
			ImplementProxyTargetAccessor(@class, interceptors);
			foreach (var attribute in targetType.GetTypeInfo().GetNonInheritableAttributes())
			{
				@class.DefineCustomAttribute(attribute.Builder);
			}
		}
		private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGenerationOptions options)
		{
			ModuleScope scope = emitter.ModuleScope;
			CacheKey key = new CacheKey(method.Method, WCFCompositionInvocationTypeGenerator.BaseType, null, null);
			Type invocation = scope.GetFromCache(key);
			if (invocation == null)
			{
				invocation = new WCFCompositionInvocationTypeGenerator(method.Method.DeclaringType, method, method.Method, false, null).Generate(emitter, options, base.namingScope).BuildType();
				scope.RegisterInCache(key, invocation);
			}
			return invocation;
		}
示例#27
0
		public void ForceUnsignedFalseWithSignedTypes()
		{
#if SILVERLIGHT // Silverlight does not allow us to sign generated assemblies
			const bool shouldBeSigned = false;
#else
			const bool shouldBeSigned = true;
#endif
			ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes,
			                                        TypeAttributes.Public, false);
			Type t = emitter.BuildType();
			Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.Assembly));
		}
		public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)
		{
			var interceptors = @class.GetField("__interceptors");
#if !SILVERLIGHT
			ImplementGetObjectData(@class);
#endif
			ImplementProxyTargetAccessor(@class, interceptors);
			foreach (var attribute in targetType.GetNonInheritableAttributes())
			{
				@class.DefineCustomAttribute(attribute);
			}
		}
示例#29
0
		protected void ImplementProxyTargetAccessor(ClassEmitter emitter, FieldReference interceptorsField)
		{
			var dynProxyGetTarget = emitter.CreateMethod("DynProxyGetTarget", typeof(object));

			dynProxyGetTarget.CodeBuilder.AddStatement(
				new ReturnStatement(new ConvertExpression(typeof(object), targetType, GetTargetReferenceExpression(emitter))));

			var getInterceptors = emitter.CreateMethod("GetInterceptors", typeof(IInterceptor[]));

			getInterceptors.CodeBuilder.AddStatement(
				new ReturnStatement(interceptorsField));
		}
		protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
		                                                      ProxyGenerationOptions options,
		                                                      OverrideMethodDelegate overrideMethod)
		{
			var invocation = GetInvocationType(method, @class, options);
			return new MethodWithInvocationGenerator(method,
			                                         @class.GetField("__interceptors"),
			                                         invocation,
			                                         (c, m) => c.GetField("__target").ToExpression(),
			                                         overrideMethod,
			                                         null);
		}