示例#1
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);
            }
        }
示例#2
0
 public NestedClassEmitter(AbstractTypeEmitter maintype, String name, Type baseType, Type[] interfaces)
     : this(
         maintype,
         CreateTypeBuilder(maintype, name, TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class,
                           baseType, interfaces))
 {
 }
示例#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);
        }
示例#4
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);
        }
示例#5
0
 private static TypeBuilder CreateTypeBuilder(AbstractTypeEmitter maintype, string name, TypeAttributes attributes,
                                              Type baseType, Type[] interfaces)
 {
     return(maintype.TypeBuilder.DefineNestedType(
                name,
                attributes,
                baseType, interfaces));
 }
示例#6
0
 internal MethodEmitter(AbstractTypeEmitter owner, String name,
                        MethodAttributes attributes, Type returnType,
                        params Type[] argumentTypes)
     : this(owner, name, attributes)
 {
     SetParameters(argumentTypes);
     SetReturnType(returnType);
 }
示例#7
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);
 }
示例#8
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);
        }
示例#9
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);
        }
示例#10
0
 protected internal ConstructorEmitter(AbstractTypeEmitter maintype, ConstructorBuilder builder)
 {
     this.maintype = maintype;
     this.builder  = builder;
 }
示例#11
0
 internal TypeConstructorEmitter(AbstractTypeEmitter maintype)
     : base(maintype, maintype.TypeBuilder.DefineTypeInitializer())
 {
 }
示例#12
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);
        }
示例#13
0
 public NestedClassEmitter(AbstractTypeEmitter maintype, TypeBuilder typeBuilder)
     : base(typeBuilder)
 {
     maintype.Nested.Add(this);
 }
示例#14
0
 internal MethodEmitter(AbstractTypeEmitter owner, String name, MethodAttributes attributes)
     : this(owner.TypeBuilder.DefineMethod(name, attributes))
 {
 }