public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, String name, PropertyAttributes attributes,
                               Type propertyType)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            // DYNPROXY-73 - AmbiguousMatchException for properties
            // This is a workaround for a framework limitation in CLR 2.0
            // This limitation was removed in CLR 2.0 SP1, but we don't want to
            // tie ourselves to that version. This perform the lookup for the new overload
            // dynamically, so we have a nice fallback on vanilla CLR 2.0

            if (TypeBuilderMethods.DefineProperty == null)
            {
                DefineProperty_Clr2_0 oldDefineProperty = parentTypeEmitter.TypeBuilder.DefineProperty;
                builder = oldDefineProperty(name, attributes, propertyType, new Type[0]);
            }
            else
            {
                DefineProperty_Clr_2_0_SP1 newDefinedProperty = (DefineProperty_Clr_2_0_SP1)
                                                                Delegate.CreateDelegate(typeof(DefineProperty_Clr_2_0_SP1),
                                                                                        parentTypeEmitter.TypeBuilder,
                                                                                        TypeBuilderMethods.DefineProperty);
                builder = newDefinedProperty(
                    name, attributes, CallingConventions.HasThis, propertyType,
                    null, null, new Type[0], null, null);
            }
        }
Example #2
0
        // private ParameterInfo[] indexParameters;

        public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes,
                               Type propertyType, Type[] arguments)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            // DYNPROXY-73 - AmbiguousMatchException for properties
            // This is a workaround for a framework limitation in CLR 2.0 
            // This limitation was removed in CLR 2.0 SP1, but we don't want to 
            // tie ourselves to that version. This perform the lookup for the new overload
            // dynamically, so we have a nice fallback on vanilla CLR 2.0

            if (TypeBuilderMethods.DefineProperty == null)
            {
                DefineProperty_Clr2_0 oldDefineProperty = parentTypeEmitter.TypeBuilder.DefineProperty;
                builder = oldDefineProperty(name, attributes, propertyType, arguments);
            }
            else
            {
                var newDefinedProperty = (DefineProperty_Clr_2_0_SP1)
                                         Delegate.CreateDelegate(typeof(DefineProperty_Clr_2_0_SP1),
                                                                 parentTypeEmitter.TypeBuilder,
                                                                 TypeBuilderMethods.DefineProperty);
                builder = newDefinedProperty(
                    name, attributes, CallingConventions.HasThis, propertyType,
                    null, null, arguments, null, null);
            }
        }
Example #3
0
        public static Type GetClosedParameterType(this AbstractTypeEmitter type, Type parameter)
        {
            if (parameter.IsGenericTypeDefinition)
            {
                return(parameter.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArgumentsFor(parameter)));
            }

            if (parameter.IsGenericType)
            {
                var arguments = parameter.GetGenericArguments();
                if (CloseGenericParametersIfAny(type, arguments))
                {
                    return(parameter.GetGenericTypeDefinition().MakeGenericType(arguments));
                }
            }

            if (parameter.IsGenericParameter)
            {
                return(type.GetGenericArgument(parameter.Name));
            }

            if (parameter.IsArray)
            {
                var elementType = GetClosedParameterType(type, parameter.GetElementType());
                return(elementType.MakeArrayType());
            }

            if (parameter.IsByRef)
            {
                var elementType = GetClosedParameterType(type, parameter.GetElementType());
                return(elementType.MakeByRefType());
            }

            return(parameter);
        }
Example #4
0
		public NestedClassEmitter(AbstractTypeEmitter maintype, String name, Type baseType, Type[] interfaces)
			: this(
				maintype,
				CreateTypeBuilder(maintype, name, TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class,
				                  baseType, interfaces))
		{
		}
 private static TypeBuilder CreateTypeBuilder(AbstractTypeEmitter maintype, string name, TypeAttributes attributes, Type baseType, Type[] interfaces)
 {
     return(maintype.TypeBuilder.DefineNestedType(
                name,
                attributes,
                baseType, interfaces));
 }
		public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
		                                                              Reference targetField,
		                                                              MethodEmitter invokeMethodOnTarget)
		{
			var @delegate = GetDelegate(invocation, invokeMethodOnTarget);
			return new MethodInvocationExpression(@delegate, GetCallbackMethod(), args);
		}
Example #7
0
        internal MethodEmitter(
            AbstractTypeEmitter owner,
            string name,
            MethodAttributes attributes,
            MethodInfo methodToUseAsATemplate
            ) : this(owner, name, attributes)
        {
            // All code paths leading up to this constructor can be traced back to
            // proxy type generation code. At present, proxy types are never generic.
            Debug.Assert(owner.GenericTypeParams == null || owner.GenericTypeParams.Length == 0);

            var returnType           = methodToUseAsATemplate.ReturnType;
            var baseMethodParameters = methodToUseAsATemplate.GetParameters();
            var parameters           = ArgumentsUtil.GetTypes(baseMethodParameters);

            genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder);
            SetParameters(parameters);
            SetReturnType(returnType);
            SetSignature(
                returnType,
                methodToUseAsATemplate.ReturnParameter,
                parameters,
                baseMethodParameters
                );
            DefineParameters(baseMethodParameters);
        }
		protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, MethodEmitter invokeMethodOnTarget, Reference targetField)
		{
			invokeMethodOnTarget.CodeBuilder.AddStatement(
				new ExpressionStatement(
					new MethodInvocationExpression(SelfReference.Self, InvocationMethods.EnsureValidTarget)));
			base.ImplementInvokeMethodOnTarget(invocation, parameters, invokeMethodOnTarget, targetField);
		}
Example #9
0
        // private ParameterInfo[] indexParameters;
        public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, String name, PropertyAttributes attributes,
		                       Type propertyType)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            builder = parentTypeEmitter.TypeBuilder.DefineProperty(name, attributes, propertyType, new Type[0]);
        }
Example #10
0
 public NestedClassEmitter(AbstractTypeEmitter maintype, String name, Type baseType, Type[] interfaces)
     : this(
         maintype,
         CreateTypeBuilder(maintype, name, TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class,
                           baseType, interfaces))
 {
 }
Example #11
0
		private static TypeBuilder CreateTypeBuilder(AbstractTypeEmitter maintype, string name, Type baseType, Type[] interfaces)
		{
			return maintype.TypeBuilder.DefineNestedType(
				name,
				TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class,
				baseType, interfaces);
		}
		private static TypeBuilder CreateTypeBuilder(AbstractTypeEmitter maintype, string name, TypeAttributes attributes, Type baseType, Type[] interfaces)
		{
			return maintype.TypeBuilder.DefineNestedType(
				name,
				attributes,
				baseType, interfaces);
		}
		public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args, Reference targetField, MethodEmitter invokeMethodOnTarget)
		{
			var allArgs = GetAllArgs(args, targetField);
			var @delegate = (Reference)invocation.GetField("delegate");

			return new MethodInvocationExpression(@delegate, GetCallbackMethod(), allArgs);
		}
Example #14
0
		public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type)
		{
			this.typeEmitter = typeEmitter;
			this.type = type;
			this.name = name;
			eventBuilder = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type);
		}
Example #15
0
		public static Type GetClosedParameterType(AbstractTypeEmitter type, Type parameter)
		{
			if (parameter.IsGenericTypeDefinition)
			{
				return parameter.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArgumentsFor(parameter));
			}

			if (parameter.IsGenericType)
			{
				Type[] arguments = parameter.GetGenericArguments();
				if (CloseGenericParametersIfAny(type, arguments))
				{
					return parameter.GetGenericTypeDefinition().MakeGenericType(arguments);
				}
			}
			
			if (parameter.IsGenericParameter)
			{
				return type.GetGenericArgument(parameter.Name);
			}

			if (parameter.IsArray)
			{
				var elementType = GetClosedParameterType(type, parameter.GetElementType());
				return elementType.MakeArrayType();
			}
			
			if(parameter.IsByRef)
			{
				var elementType = GetClosedParameterType(type, parameter.GetElementType());
				return elementType.MakeByRefType();
			}

			return parameter;
		}
Example #16
0
		public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type)
		{
			if (name == null) throw new ArgumentNullException("name");
			if (type == null) throw new ArgumentNullException("type");
			this.typeEmitter = typeEmitter;
			this.type = type;
			eventBuilder = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type);
		}
Example #17
0
		internal MethodEmitter(AbstractTypeEmitter owner, String name,
		                       MethodAttributes attributes, Type returnType,
		                       params Type[] argumentTypes)
			: this(owner, name, attributes)
		{
			SetParameters(argumentTypes);
			SetReturnType(returnType);
		}
Example #18
0
		internal ConstructorEmitter(AbstractTypeEmitter maintype, params ArgumentReference[] arguments)
		{
			this.maintype = maintype;

			var args = ArgumentsUtil.InitializeAndConvert(arguments);

			builder = maintype.TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, args);
		}
Example #19
0
 internal MethodEmitter(AbstractTypeEmitter owner, String name,
                        MethodAttributes attributes, Type returnType,
                        params Type[] argumentTypes)
     : this(owner, name, attributes)
 {
     SetParameters(argumentTypes);
     SetReturnType(returnType);
 }
Example #20
0
        internal ConstructorEmitter(AbstractTypeEmitter maintype, params ArgumentReference[] arguments)
        {
            this.maintype = maintype;

            var args = ArgumentsUtil.InitializeAndConvert(arguments);

            builder = maintype.TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, args);
        }
Example #21
0
 protected internal ConstructorEmitter(
     AbstractTypeEmitter maintype,
     ConstructorBuilder builder
     )
 {
     this.maintype = maintype;
     this.builder  = builder;
     codeBuilder   = new CodeBuilder();
 }
Example #22
0
		public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes,
		                       Type propertyType, Type[] arguments)
		{
			this.parentTypeEmitter = parentTypeEmitter;

			builder = parentTypeEmitter.TypeBuilder.DefineProperty(
				name, attributes, CallingConventions.HasThis, propertyType,
				null, null, arguments, null, null);
		}
Example #23
0
        public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes,
                               Type propertyType, Type[] arguments)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            builder = parentTypeEmitter.TypeBuilder.DefineProperty(
                name, attributes, CallingConventions.HasThis, propertyType,
                null, null, arguments, null, null);
        }
		public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
		{
			var arguments = GetArguments(baseCtorArguments);
			var constructor = invocation.CreateConstructor(arguments);

			var delegateField = invocation.CreateField("delegate", delegateType);
			constructor.CodeBuilder.AddStatement(new AssignStatement(delegateField, new ReferenceExpression(arguments[0])));
			return constructor;
		}
Example #25
0
 public NestedClassEmitter(
     AbstractTypeEmitter maintype,
     string name,
     TypeAttributes attributes,
     Type baseType,
     Type[] interfaces
     ) : this(maintype, CreateTypeBuilder(maintype, name, attributes, baseType, interfaces))
 {
 }
		private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
		{
			var closedDelegateType = delegateType.MakeGenericType(invocation.GenericTypeParams);
			var localReference = invokeMethodOnTarget.CodeBuilder.DeclareLocal(closedDelegateType);
			var closedMethodOnTarget = method.MethodOnTarget.MakeGenericMethod(invocation.GenericTypeParams);
			var localTarget = new ReferenceExpression(targetReference);
			invokeMethodOnTarget.CodeBuilder.AddStatement(
				SetDelegate(localReference, localTarget, closedDelegateType, closedMethodOnTarget));
			return localReference;
		}
Example #27
0
		public static void PopulateGenericArguments(
			AbstractTypeEmitter parentEmitter,
			Dictionary<String, GenericTypeParameterBuilder> name2GenericType)
		{
			if (parentEmitter.GenericTypeParams == null) return;

			foreach (GenericTypeParameterBuilder genType in parentEmitter.GenericTypeParams)
			{
				name2GenericType.Add(genType.Name, genType);
			}
		}
        public static void PopulateGenericArguments(
            AbstractTypeEmitter parentEmitter,
            Dictionary <String, GenericTypeParameterBuilder> name2GenericType)
        {
            if (parentEmitter.GenericTypeParams == null)
            {
                return;
            }

            foreach (GenericTypeParameterBuilder genType in parentEmitter.GenericTypeParams)
            {
                name2GenericType.Add(genType.Name, genType);
            }
        }
Example #29
0
		public static Dictionary<string, GenericTypeParameterBuilder> GetGenericArgumentsMap(AbstractTypeEmitter parentEmitter)
		{
			if (parentEmitter.GenericTypeParams == null || parentEmitter.GenericTypeParams.Length == 0)
			{
				return new Dictionary<string, GenericTypeParameterBuilder>(0);
			}

			var name2GenericType = new Dictionary<string, GenericTypeParameterBuilder>(parentEmitter.GenericTypeParams.Length);
			foreach (var genType in parentEmitter.GenericTypeParams)
			{
				name2GenericType.Add(genType.Name, genType);
			}
			return name2GenericType;
		}
Example #30
0
 public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     this.typeEmitter = typeEmitter;
     this.type        = type;
     eventBuilder     = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type);
 }
Example #31
0
        private static bool CloseGenericParametersIfAny(AbstractTypeEmitter emitter, Type[] arguments)
        {
            var hasAnyGenericParameters = false;

            for (var i = 0; i < arguments.Length; i++)
            {
                var newType = GetClosedParameterType(emitter, arguments[i]);
                if (!ReferenceEquals(newType, arguments[i]))
                {
                    arguments[i]            = newType;
                    hasAnyGenericParameters = true;
                }
            }
            return(hasAnyGenericParameters);
        }
Example #32
0
		/// <summary>
		/// Inspect the base method for generic definitions
		/// and set the return type and the parameters
		/// accordingly
		/// </summary>
		public void CopyParametersAndReturnTypeFrom(MethodInfo baseMethod, AbstractTypeEmitter parentEmitter)
		{
			GenericUtil.PopulateGenericArguments(parentEmitter, name2GenericType);
			Type returnType = GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType);
			ParameterInfo[] baseMethodParameters = baseMethod.GetParameters();
			Type[] parameters = GenericUtil.ExtractParametersTypes
				(baseMethodParameters, name2GenericType);

			// Disabled due to .Net 3.5 SP 1 bug
//			List<Type[]> paramModReq = new List<Type[]>();
//			List<Type[]> paramModOpt = new List<Type[]>();
//			foreach (ParameterInfo parameterInfo in baseMethodParameters)
//			{
//				paramModOpt.Add(parameterInfo.GetOptionalCustomModifiers());
//				paramModReq.Add(parameterInfo.GetRequiredCustomModifiers());
//			} 

			Type[] genericArguments = baseMethod.GetGenericArguments();

			genericTypeParams = GenericUtil.DefineGenericArguments(genericArguments, builder, name2GenericType);
			// Bind parameter types

			SetParameters(GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType));

			// TODO: check if the return type is a generic
			// definition for the method

			SetReturnType(GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType));

#if SILVERLIGHT
#warning What to do?
#else
			builder.SetSignature(
				returnType,
				// Disabled due to .Net 3.5 SP 1 bug
				//baseMethod.ReturnParameter.GetRequiredCustomModifiers(),
				//baseMethod.ReturnParameter.GetOptionalCustomModifiers(),
				Type.EmptyTypes,
				Type.EmptyTypes,
				parameters,
				null, null
//				 paramModReq.ToArray(),
//				 paramModOpt.ToArray()
				);
#endif

			DefineParameters(baseMethodParameters);
		}
Example #33
0
        internal MethodEmitter(AbstractTypeEmitter owner, String name,
                               MethodAttributes attributes, MethodInfo methodToUseAsATemplate)
            : this(owner, name, attributes)
        {
            var name2GenericType = GenericUtil.GetGenericArgumentsMap(owner);

            var returnType           = GenericUtil.ExtractCorrectType(methodToUseAsATemplate.ReturnType, name2GenericType);
            var baseMethodParameters = methodToUseAsATemplate.GetParameters();
            var parameters           = GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType);

            genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder, name2GenericType);
            SetParameters(parameters);
            SetReturnType(returnType);
            SetSignature(returnType, methodToUseAsATemplate.ReturnParameter, parameters, baseMethodParameters);
            DefineParameters(baseMethodParameters);
        }
Example #34
0
		internal MethodEmitter(AbstractTypeEmitter owner, String name,
		                       MethodAttributes attributes, MethodInfo methodToUseAsATemplate)
			: this(owner, name, attributes)
		{
			var name2GenericType = GenericUtil.GetGenericArgumentsMap(owner);

			var returnType = GenericUtil.ExtractCorrectType(methodToUseAsATemplate.ReturnType, name2GenericType);
			var baseMethodParameters = methodToUseAsATemplate.GetParameters();
			var parameters = GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType);

			genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder, name2GenericType);
			SetParameters(parameters);
			SetReturnType(returnType);
			SetSignature(returnType, methodToUseAsATemplate.ReturnParameter, parameters, baseMethodParameters);
			DefineParameters(baseMethodParameters);
		}
Example #35
0
		/// <summary>
		/// Inspect the base method for generic definitions
		/// and set the return type and the parameters
		/// accordingly
		/// </summary>
		public void CopyParametersAndReturnTypeFrom(MethodInfo baseMethod, AbstractTypeEmitter parentEmitter)
		{
			GenericUtil.PopulateGenericArguments(parentEmitter, name2GenericType);
			var returnType = GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType);
			var baseMethodParameters = baseMethod.GetParameters();
			var parameters = GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType);

			genericTypeParams = GenericUtil.CopyGenericArguments(baseMethod, builder, name2GenericType);

			// Bind parameter types
			// TODO: check if the return type is a generic
			// definition for the method

			SetParameters(parameters);
			SetReturnType(returnType);
			DefineParameters(baseMethodParameters);
		}
        /// <summary>
        /// Inspect the base method for generic definitions
        /// and set the return type and the parameters
        /// accordingly
        /// </summary>
        public void CopyParametersAndReturnTypeFrom(MethodInfo baseMethod, AbstractTypeEmitter parentEmitter)
        {
            GenericUtil.PopulateGenericArguments(parentEmitter, name2GenericType);
            Type returnType = GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType);

            ParameterInfo[] baseMethodParameters = baseMethod.GetParameters();
            Type[]          parameters           = GenericUtil.ExtractParametersTypes
                                                       (baseMethodParameters, name2GenericType);

            //// Disabled for .NET due to .Net 3.5 SP 1 bug
            //List<Type[]> paramModReq = new List<Type[]>();
            //List<Type[]> paramModOpt = new List<Type[]>();
            //foreach (ParameterInfo parameterInfo in baseMethodParameters)
            //{
            //    paramModOpt.Add(parameterInfo.GetOptionalCustomModifiers());
            //    paramModReq.Add(parameterInfo.GetRequiredCustomModifiers());
            //}

            genericTypeParams = GenericUtil.CopyGenericArguments(baseMethod, builder, name2GenericType);
            // Bind parameter types

            SetParameters(GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType));

            // TODO: check if the return type is a generic
            // definition for the method

            SetReturnType(GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType));

            builder.SetSignature(
                returnType,
                // Disabled due to .Net 3.5 SP 1 bug and Silverlight does not have this API
                //baseMethod.ReturnParameter.GetRequiredCustomModifiers(),
                //baseMethod.ReturnParameter.GetOptionalCustomModifiers(),
                Type.EmptyTypes,
                Type.EmptyTypes,
                parameters,
                null, null
//				 paramModReq.ToArray(),
//				 paramModOpt.ToArray()
                );


            DefineParameters(baseMethodParameters);
        }
Example #37
0
		/// <summary>
		/// Inspect the base method for generic definitions
		/// and set the return type and the parameters
		/// accordingly
		/// </summary>
		public void CopyParametersAndReturnTypeFrom(MethodInfo baseMethod, AbstractTypeEmitter parentEmitter)
		{
			GenericUtil.PopulateGenericArguments(parentEmitter, name2GenericType);
			Type returnType = GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType);
			ParameterInfo[] baseMethodParameters = baseMethod.GetParameters();
			Type[] parameters = GenericUtil.ExtractParametersTypes
				(baseMethodParameters, name2GenericType);

			//// Disabled for .NET due to .Net 3.5 SP 1 bug
			//List<Type[]> paramModReq = new List<Type[]>();
			//List<Type[]> paramModOpt = new List<Type[]>();
			//foreach (ParameterInfo parameterInfo in baseMethodParameters)
			//{
			//    paramModOpt.Add(parameterInfo.GetOptionalCustomModifiers());
			//    paramModReq.Add(parameterInfo.GetRequiredCustomModifiers());
			//} 

			genericTypeParams = GenericUtil.CopyGenericArguments(baseMethod, builder, name2GenericType);
			// Bind parameter types

			SetParameters(GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType));

			// TODO: check if the return type is a generic
			// definition for the method

			SetReturnType(GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType));

			builder.SetSignature(
				returnType,
				// Disabled due to .Net 3.5 SP 1 bug and Silverlight does not have this API
				//baseMethod.ReturnParameter.GetRequiredCustomModifiers(),
				//baseMethod.ReturnParameter.GetOptionalCustomModifiers(),
				Type.EmptyTypes,
				Type.EmptyTypes,
				parameters,
				null, null
//				 paramModReq.ToArray(),
//				 paramModOpt.ToArray()
				);


			DefineParameters(baseMethodParameters);
		}
Example #38
0
		/// <summary>
		/// Inspect the base method for generic definitions
		/// and set the return type and the parameters
		/// accordingly
		/// </summary>
		public void CopyParametersAndReturnTypeFrom(MethodInfo baseMethod, AbstractTypeEmitter parentEmitter)
		{
			GenericUtil.PopulateGenericArguments(parentEmitter, name2GenericType); 
			Type returnType = GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType);
			ParameterInfo[] baseMethodParameters = baseMethod.GetParameters();
			Type[] parameters = GenericUtil.ExtractParametersTypes
		   (baseMethodParameters, name2GenericType);

			List<Type[]> paramModReq = new List<Type[]>();
			List<Type[]> paramModOpt = new List<Type[]>();
			foreach (ParameterInfo parameterInfo in baseMethodParameters)
			{
				paramModOpt.Add(parameterInfo.GetOptionalCustomModifiers());
				paramModReq.Add(parameterInfo.GetRequiredCustomModifiers());
			} 

			Type[] genericArguments = baseMethod.GetGenericArguments();

			genericTypeParams = GenericUtil.DefineGenericArguments(genericArguments, builder, name2GenericType);
			// Bind parameter types

			SetParameters(GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType));

			// TODO: check if the return type is a generic
			// definition for the method

			SetReturnType(GenericUtil.ExtractCorrectType(baseMethod.ReturnType, name2GenericType));

			builder.SetSignature(
				 returnType,
				 baseMethod.ReturnParameter.GetRequiredCustomModifiers(),
				 baseMethod.ReturnParameter.GetOptionalCustomModifiers(),
				 parameters,
				 paramModReq.ToArray(),
				 paramModOpt.ToArray());

			DefineParameters(baseMethodParameters);
		}
Example #39
0
        // private ParameterInfo[] indexParameters;
        public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes,
		                       Type propertyType, Type[] arguments)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            // DYNPROXY-73 - AmbiguousMatchException for properties
            // This is a workaround for a framework limitation in CLR 2.0
            // This limitation was removed in CLR 2.0 SP1, but we don't want to
            // tie ourselves to that version. This perform the lookup for the new overload
            // dynamically, so we have a nice fallback on vanilla CLR 2.0

            // @mbrit - 2012-05-30 - it appears in WinRT, the old version works OK... new version is baffling...
            #if !NETFX_CORE
            bool isWinRt = false;
            #else
            bool isWinRt = true;
            #endif

            if (TypeBuilderMethods.DefineProperty == null || isWinRt)
            {
                DefineProperty_Clr2_0 oldDefineProperty = parentTypeEmitter.TypeBuilder.DefineProperty;
                builder = oldDefineProperty(name, attributes, propertyType, arguments);
            }
            else
            {
            #if !NETFX_CORE
                var newDefinedProperty = (DefineProperty_Clr_2_0_SP1)
                                         Delegate.CreateDelegate(typeof(DefineProperty_Clr_2_0_SP1),
                                                                 parentTypeEmitter.TypeBuilder,
                                                                 TypeBuilderMethods.DefineProperty);

                builder = newDefinedProperty(
                    name, attributes, CallingConventions.HasThis, propertyType,
                    null, null, arguments, null, null);
            #endif
            }
        }
        // private ParameterInfo[] indexParameters;

        public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes,
                               Type propertyType, Type[] arguments)
        {
            this.parentTypeEmitter = parentTypeEmitter;

            // DYNPROXY-73 - AmbiguousMatchException for properties
            // This is a workaround for a framework limitation in CLR 2.0
            // This limitation was removed in CLR 2.0 SP1, but we don't want to
            // tie ourselves to that version. This perform the lookup for the new overload
            // dynamically, so we have a nice fallback on vanilla CLR 2.0

            // @mbrit - 2012-05-30 - it appears in WinRT, the old version works OK... new version is baffling...
#if !NETFX_CORE
            bool isWinRt = false;
#else
            bool isWinRt = true;
#endif

            if (TypeBuilderMethods.DefineProperty == null || isWinRt)
            {
                DefineProperty_Clr2_0 oldDefineProperty = parentTypeEmitter.TypeBuilder.DefineProperty;
                builder = oldDefineProperty(name, attributes, propertyType, arguments);
            }
            else
            {
#if !NETFX_CORE
                var newDefinedProperty = (DefineProperty_Clr_2_0_SP1)
                                         Delegate.CreateDelegate(typeof(DefineProperty_Clr_2_0_SP1),
                                                                 parentTypeEmitter.TypeBuilder,
                                                                 TypeBuilderMethods.DefineProperty);

                builder = newDefinedProperty(
                    name, attributes, CallingConventions.HasThis, propertyType,
                    null, null, arguments, null, null);
#endif
            }
        }
Example #41
0
		internal MethodEmitter(AbstractTypeEmitter owner, String name, MethodAttributes attributes)
			: this(owner.TypeBuilder.DefineMethod(name, attributes))
		{
		}
Example #42
0
		private static bool CloseGenericParametersIfAny(AbstractTypeEmitter emitter, Type[] arguments)
		{
			var hasAnyGenericParameters = false;
			for (var i = 0; i < arguments.Length; i++)
			{
				var newType = GetClosedParameterType(emitter, arguments[i]);
				if (!ReferenceEquals(newType, arguments[i]))
				{
					arguments[i] = newType;
					hasAnyGenericParameters = true;
				}
			}
			return hasAnyGenericParameters;
		}
		internal TypeConstructorEmitter(AbstractTypeEmitter maintype)
			: base(maintype, maintype.TypeBuilder.DefineTypeInitializer())
		{
		}
Example #44
0
        public static Dictionary <string, GenericTypeParameterBuilder> GetGenericArgumentsMap(AbstractTypeEmitter parentEmitter)
        {
            if (parentEmitter.GenericTypeParams == null || parentEmitter.GenericTypeParams.Length == 0)
            {
                return(new Dictionary <string, GenericTypeParameterBuilder>(0));
            }

            var name2GenericType = new Dictionary <string, GenericTypeParameterBuilder>(parentEmitter.GenericTypeParams.Length);

            foreach (var genType in parentEmitter.GenericTypeParams)
            {
                name2GenericType.Add(genType.Name, genType);
            }
            return(name2GenericType);
        }
Example #45
0
 internal MethodEmitter(AbstractTypeEmitter owner, String name, MethodAttributes attributes)
     : this(owner.TypeBuilder.DefineMethod(name, attributes))
 {
 }
 internal MethodEmitter(AbstractTypeEmitter maintype, String name, MethodAttributes attrs)
     : this(maintype.TypeBuilder.DefineMethod(name, attrs))
 {
 }
 internal TypeConstructorEmitter(AbstractTypeEmitter mainType)
     : base(mainType, mainType.TypeBuilder.DefineTypeInitializer())
 {
 }
		public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
		{
			return invocation.CreateConstructor(baseCtorArguments);
		}
		public NestedClassEmitter(AbstractTypeEmitter maintype, TypeBuilder typeBuilder)
			: base(typeBuilder)
		{
			maintype.Nested.Add(this);
		}
Example #50
0
 public NestedClassEmitter(AbstractTypeEmitter maintype, TypeBuilder typeBuilder)
     : base(typeBuilder)
 {
     maintype.Nested.Add(this);
 }
Example #51
0
		internal MethodEmitter(AbstractTypeEmitter maintype, String name, MethodAttributes attrs)
			: this (maintype.TypeBuilder.DefineMethod(name, attrs))
		{
			this.maintype = maintype;
		}