Exemple #1
0
        /// <summary>
        /// 使用当前的构造器定义一个动态类型。
        /// </summary>
        /// <param name="typeName">类型的名称。</param>
        /// <param name="visual">指定类的可见性。</param>
        /// <param name="calling">指定类的调用属性。</param>
        /// <param name="baseType">类型的父类。</param>
        /// <returns></returns>
        public DynamicTypeBuilder DefineType(string typeName, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Type baseType = null)
        {
            var typeBuilder = new DynamicTypeBuilder(Context, typeName, visual, calling, baseType);

            _typeBuilders.Add(typeBuilder);
            return(typeBuilder);
        }
Exemple #2
0
        /// <summary>
        /// 定义一个嵌套的类型。
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="visual"></param>
        /// <param name="baseType"></param>
        /// <returns></returns>
        public virtual DynamicTypeBuilder DefineNestedType(string typeName, VisualDecoration visual = VisualDecoration.Private, Type baseType = null)
        {
            var nestedType = new DynamicTypeBuilder(Context, typeName, visual, baseType);

            nestedTypeBuilders.Add(nestedType);
            return(nestedType);
        }
Exemple #3
0
        /// <summary>
        /// 使用当前的构造器定义一个动态接口。
        /// </summary>
        /// <param name="typeName">类型的名称。</param>
        /// <param name="visual">指定类的可见性。</param>
        /// <returns></returns>
        public DynamicInterfaceBuilder DefineNestedInterface(string typeName, VisualDecoration visual = VisualDecoration.Public)
        {
            var typeBuilder = new DynamicInterfaceBuilder(Context, typeName, visual);

            nestedTypeBuilders.Add(typeBuilder);
            return(typeBuilder);
        }
Exemple #4
0
        private FieldAttributes GetAttributes(VisualDecoration visual, CallingDecoration calling)
        {
            var attrs = FieldAttributes.HasDefault;

            switch (calling)
            {
            case CallingDecoration.Static:
                attrs |= FieldAttributes.Static;
                break;
            }

            switch (visual)
            {
            case VisualDecoration.Internal:
                attrs |= FieldAttributes.Assembly;
                break;

            case VisualDecoration.Private:
                attrs |= FieldAttributes.Private;
                break;

            case VisualDecoration.Public:
                attrs |= FieldAttributes.Public;
                break;
            }

            return(attrs);
        }
Exemple #5
0
 /// <summary>
 /// 初始化 <see cref="DynamicTypeBuilder"/> 类的新实例。
 /// </summary>
 /// <param name="context"></param>
 /// <param name="typeName">动态类型的名称。</param>
 /// <param name="visual">指定类的可见性。</param>
 /// <param name="calling">指定类的调用属性。</param>
 /// <param name="baseType">动态类型继承的基类。</param>
 internal DynamicTypeBuilder(BuildContext context, string typeName, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Type baseType = null)
 {
     Context = new BuildContext(context)
     {
         TypeBuilder = this
     };
     TypeName      = typeName;
     this.baseType = baseType;
     attributes    = GetTypeAttributes(visual, calling);
     InitBuilder();
 }
Exemple #6
0
 internal DynamicTypeBuilder(BuildContext context, string typeName, VisualDecoration visual, Type baseType)
 {
     Context = new BuildContext(context)
     {
         TypeBuilder = context.TypeBuilder
     };
     isNesetType   = true;
     TypeName      = typeName;
     this.baseType = baseType;
     attributes    = GetTypeAttributes(visual, CallingDecoration.Standard);
     InitBuilder();
 }
        private TypeAttributes GetTypeAttributes(VisualDecoration visual)
        {
            var attrs = TypeAttributes.Class;

            switch (visual)
            {
            case VisualDecoration.Internal:
                break;

            case VisualDecoration.Public:
                attrs |= TypeAttributes.Public;
                break;
            }

            return(attrs);
        }
        private MethodAttributes GetMethodAttributes(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard)
        {
            var attributes = Context.TypeBuilder.GetMethodAttributes();

            switch (calling)
            {
            case CallingDecoration.Abstract:
                attributes |= MethodAttributes.Abstract | MethodAttributes.Virtual | MethodAttributes.NewSlot;
                break;

            case CallingDecoration.Virtual:
                attributes |= MethodAttributes.Virtual | MethodAttributes.NewSlot;
                break;

            case CallingDecoration.Sealed:
                attributes |= MethodAttributes.Final;
                break;

            case CallingDecoration.Static:
                attributes |= MethodAttributes.Static;
                break;

            case CallingDecoration.ExplicitImpl:
                attributes |= MethodAttributes.Private | MethodAttributes.Final;
                break;
            }

            switch (visual)
            {
            case VisualDecoration.Internal:
                attributes |= MethodAttributes.Assembly;
                break;

            case VisualDecoration.Public:
                if (calling != CallingDecoration.ExplicitImpl)
                {
                    attributes |= MethodAttributes.Public;
                }
                break;

            case VisualDecoration.Protected:
                attributes |= MethodAttributes.Family;
                break;
            }

            return(attributes);
        }
        /// <summary>
        /// 定义属性的 Get 访问方法。
        /// </summary>
        /// <param name="visual">指定方法的可见性。</param>
        /// <param name="calling">指定方法的调用属性。</param>
        /// <param name="fieldBuilder">指定一个属性相关的 <see cref="DynamicFieldBuilder"/>。</param>
        /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns>
        public DynamicMethodBuilder DefineGetMethodByField(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, DynamicFieldBuilder fieldBuilder = null)
        {
            var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder;
            var method      = new DynamicMethodBuilder(Context, "get_" + Name, PropertyType, Type.EmptyTypes, ilCoding: ctx =>
            {
                if (isInterface)
                {
                    return;
                }

                if (fieldBuilder == null)
                {
                    fieldBuilder = FieldBuilder;
                }

                ctx.Emitter.ldarg_0.ldfld(fieldBuilder.FieldBuilder).ret();
            });

            PropertyBuilder.SetGetMethod(method.MethodBuilder);
            return(method);
        }
Exemple #10
0
        /// <summary>
        /// 定义属性的 Set 访问方法。
        /// </summary>
        /// <param name="visual">指定方法的可见性。</param>
        /// <param name="calling">指定方法的调用属性。</param>
        /// <param name="fieldBuilder">指定一个属性相关的 <see cref="DynamicFieldBuilder"/>。</param>
        /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns>
        public DynamicMethodBuilder DefineSetMethodByField(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, DynamicFieldBuilder fieldBuilder = null)
        {
            var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder;
            var method      = new DynamicMethodBuilder(Context, string.Concat("set_", GetMethodName()), null, new[] { PropertyType }, visual, calling, ctx =>
            {
                if (isInterface)
                {
                    return;
                }

                if (fieldBuilder == null)
                {
                    fieldBuilder = FieldBuilder;
                }

                ctx.Emitter.ldarg_0.ldarg_1.stfld(fieldBuilder.FieldBuilder).ret();
            });

            PropertyBuilder.SetSetMethod(method.MethodBuilder);
            return(method);
        }
Exemple #11
0
        private TypeAttributes GetTypeAttributes(VisualDecoration visual, CallingDecoration calling)
        {
            var attrs = GetTypeAttributes();

            switch (calling)
            {
            case CallingDecoration.Abstract:
                attrs |= TypeAttributes.Abstract;
                break;

            case CallingDecoration.Sealed:
                attrs |= TypeAttributes.Sealed;
                break;
            }

            switch (visual)
            {
            case VisualDecoration.Internal:
                if (isNesetType)
                {
                    attrs |= TypeAttributes.NestedAssembly;
                }

                break;

            case VisualDecoration.Private:
                if (isNesetType)
                {
                    attrs |= TypeAttributes.NestedPrivate;
                }

                break;

            case VisualDecoration.Public:
                attrs |= isNesetType ? TypeAttributes.NestedPublic : TypeAttributes.Public;
                break;
            }

            return(attrs);
        }
        /// <summary>
        /// 定义属性的 Set 访问方法。
        /// </summary>
        /// <param name="visual">指定方法的可见性。</param>
        /// <param name="calling">指定方法的调用属性。</param>
        /// <param name="ilCoding">方法体的 IL 过程。</param>
        /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns>
        public DynamicMethodBuilder DefineSetMethod(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
        {
            var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder;
            var method      = new DynamicMethodBuilder(Context, "set_" + Name, null, new[] { PropertyType }, ilCoding: ctx =>
            {
                if (isInterface)
                {
                    return;
                }

                if (ilCoding != null)
                {
                    ilCoding(ctx);
                }
                else
                {
                    ctx.Emitter.ldarg_0.ldarg_1.stfld(FieldBuilder.FieldBuilder).ret();
                }
            });

            PropertyBuilder.SetSetMethod(method.MethodBuilder);
            return(method);
        }
Exemple #13
0
        /// <summary>
        /// 定义属性的 Get 访问方法。
        /// </summary>
        /// <param name="visual">指定方法的可见性。</param>
        /// <param name="calling">指定方法的调用属性。</param>
        /// <param name="ilCoding">方法体的 IL 过程。</param>
        /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns>
        public DynamicMethodBuilder DefineGetMethod(VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
        {
            var isInterface = Context.TypeBuilder is DynamicInterfaceBuilder;
            var method      = new DynamicMethodBuilder(Context, string.Concat("get_", GetMethodName()), PropertyType, Type.EmptyTypes, visual, calling, ctx =>
            {
                if (isInterface)
                {
                    return;
                }

                if (ilCoding != null)
                {
                    ilCoding(ctx);
                }
                else
                {
                    ctx.Emitter.ldarg_0.ldfld(FieldBuilder.FieldBuilder).ret();
                }
            });

            PropertyBuilder.SetGetMethod(method.MethodBuilder);
            return(method);
        }
Exemple #14
0
 /// <summary>
 /// 定义一个字段。
 /// </summary>
 /// <param name="fieldName">字段的名称。</param>
 /// <param name="fieldType">字段的类型。</param>
 /// <param name="defaultValue">默认值。</param>
 /// <param name="visual"></param>
 /// <param name="calling"></param>
 /// <returns></returns>
 public virtual DynamicFieldBuilder DefineField(string fieldName, Type fieldType, object defaultValue = null, VisualDecoration visual = VisualDecoration.Private, CallingDecoration calling = CallingDecoration.Standard)
 {
     return(new DynamicFieldBuilder(Context, fieldName, fieldType, defaultValue, visual, calling));
 }
Exemple #15
0
 /// <summary>
 /// 定义一个构造函数。
 /// </summary>
 /// <param name="parameterTypes"></param>
 /// <param name="visual"></param>
 /// <param name="calling"></param>
 /// <param name="ilCoding"></param>
 /// <returns></returns>
 public virtual DynamicConstructorBuilder DefineConstructor(Type[] parameterTypes, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
 {
     return(new DynamicConstructorBuilder(Context, parameterTypes, visual, calling, ilCoding));
 }
Exemple #16
0
 /// <summary>
 /// 定义一个方法。
 /// </summary>
 /// <param name="methodName">方法的名称。</param>
 /// <param name="returnType">返回值的类型,如果为 void 则该参数为 null。</param>
 /// <param name="parameterTypes">一个数组,表示方法的传入参数类型。</param>
 /// <param name="visual">指定方法的可见性。</param>
 /// <param name="calling">指定方法的调用属性。</param>
 /// <param name="ilCoding">方法体的 IL 过程。</param>
 /// <returns>新的 <see cref="DynamicMethodBuilder"/>。</returns>
 public virtual DynamicMethodBuilder DefineMethod(string methodName, Type returnType = null, Type[] parameterTypes = null, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
 {
     return(new DynamicMethodBuilder(Context, methodName, returnType, parameterTypes, visual, calling, ilCoding));
 }
Exemple #17
0
 public DynamicBuilder(VisualDecoration visual, CallingDecoration calling)
 {
     Visual  = visual;
     Calling = calling;
 }
        private MethodAttributes GetMethodAttributes(string methodName, IEnumerable <Type> parameterTypes, VisualDecoration visual, CallingDecoration calling)
        {
            var method       = FindMethod(methodName, parameterTypes);
            var isOverride   = method != null && method.IsVirtual;
            var isInterface1 = isOverride && method.DeclaringType.IsInterface;
            var isBaseType   = isOverride && method.DeclaringType == Context.TypeBuilder.BaseType;

            if (method != null)
            {
                CheckGenericMethod(method);
                Context.BaseMethod = method;
            }

            var attrs = GetMethodAttributes(visual, calling);

            if (isOverride)
            {
                attrs |= MethodAttributes.Virtual;

                //去掉 NewSlot
                if (isBaseType && attributes.HasFlag(MethodAttributes.NewSlot))
                {
                    attrs &= ~MethodAttributes.NewSlot;
                }
                else if (isInterface1)
                {
                    //如果没有传入 calling,则加 Final 去除上面定义的 Virtual
                    if (calling == CallingDecoration.Standard)
                    {
                        attrs |= MethodAttributes.Final;
                    }

                    attrs |= MethodAttributes.NewSlot;
                }
            }
            else if (method != null)
            {
            }

            return(attrs);
        }
Exemple #19
0
 /// <summary>
 /// 定义一个字段。
 /// </summary>
 /// <param name="fieldName">字段的名称。</param>
 /// <param name="fieldType">字段的类型。</param>
 /// <param name="defaultValue"></param>
 /// <param name="visual"></param>
 /// <param name="calling"></param>
 /// <returns></returns>
 public override DynamicFieldBuilder DefineField(string fieldName, Type fieldType, object defaultValue = null, VisualDecoration visual = VisualDecoration.Private, CallingDecoration calling = CallingDecoration.Standard)
 {
     return(null);
 }
 internal DynamicEnumBuilder(BuildContext context, string enumName, Type underlyingType, VisualDecoration visual = VisualDecoration.Public)
     : base(visual, CallingDecoration.Standard)
 {
     Context = new BuildContext(context)
     {
         EnumBuilder = this
     };
     EnumName       = enumName;
     UnderlyingType = underlyingType;
     attributes     = GetTypeAttributes(visual);
     InitBuilder();
 }
        internal DynamicConstructorBuilder(BuildContext context, Type[] parameterTypes, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
            : base(visual, calling)
        {
            Context = new BuildContext(context)
            {
                ConstructorBuilder = this
            };
            ParameterTypes = parameterTypes;
            if (ilCoding == null)
            {
                if (context.TypeBuilder.BaseType != null)
                {
                    var baseCon = parameterTypes == null?
                                  context.TypeBuilder.BaseType.GetConstructors().FirstOrDefault() :
                                      context.TypeBuilder.BaseType.GetConstructor(parameterTypes);

                    if (baseCon != null)
                    {
                        ilCoding = c => c.Emitter.ldarg_0
                                   .Assert(parameterTypes != null, b => b.For(0, parameterTypes.Length, (e, i) => e.ldarg(i + 1)))
                                   .call(baseCon).ret();
                    }
                }
                else
                {
                    ilCoding = c => c.Emitter.ret();
                }
            }

            _buildAction = ilCoding;
            _attributes  = GetMethodAttributes(visual, calling);
            InitBuilder();
        }
Exemple #22
0
 internal DynamicFieldBuilder(BuildContext context, string fieldName, Type fieldType, object defaultValue = null, VisualDecoration visual = VisualDecoration.Private, CallingDecoration calling = CallingDecoration.Standard)
     : base(visual, calling)
 {
     FieldName         = fieldName;
     FieldType         = fieldType;
     attributes        = GetAttributes(visual, calling);
     this.defaultValue = defaultValue;
     Context           = context;
     InitBuilder();
 }
Exemple #23
0
        /// <summary>
        /// 使用当前构造器定义一个枚举。
        /// </summary>
        /// <param name="enumName">枚举的名称。</param>
        /// <param name="underlyingType">枚举的类型。</param>
        /// <param name="visual">指定枚举的可见性。</param>
        /// <returns></returns>
        public DynamicEnumBuilder DefineNestedEnum(string enumName, Type underlyingType = null, VisualDecoration visual = VisualDecoration.Public)
        {
            var enumBuilder = new DynamicEnumBuilder(Context, enumName, underlyingType ?? typeof(int), visual);

            nestedTypeBuilders.Add(enumBuilder);
            return(enumBuilder);
        }
 internal DynamicMethodBuilder(BuildContext context, string methodName, Type returnType = null, Type[] parameterTypes = null, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard, Action <BuildContext> ilCoding = null)
     : base(visual, calling)
 {
     Context = new BuildContext(context)
     {
         MethodBuilder = this
     };
     Name           = methodName;
     ReturnType     = returnType;
     ParameterTypes = parameterTypes;
     this.action    = ilCoding;
     attributes     = GetMethodAttributes(methodName, parameterTypes, visual, calling);
     InitBuilder();
 }
Exemple #25
0
 /// <summary>
 /// 定义一个属性。
 /// </summary>
 /// <param name="propertyName">属性的名称。</param>
 /// <param name="propertyType">属性的类型。</param>
 /// <param name="visual">指定属性的可见性。</param>
 /// <param name="calling">指定属性的调用属性。</param>
 /// <returns>新的 <see cref="DynamicPropertyBuilder"/>。</returns>
 public virtual DynamicPropertyBuilder DefineProperty(string propertyName, Type propertyType, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard)
 {
     return(new DynamicPropertyBuilder(Context, propertyName, propertyType, visual, calling));
 }
Exemple #26
0
 internal DynamicInterfaceBuilder(BuildContext context, string typeName, VisualDecoration visual) :
     base(context, typeName, visual, CallingDecoration.Standard, null)
 {
 }
Exemple #27
0
 internal DynamicPropertyBuilder(BuildContext context, string propertyName, Type propertyType, VisualDecoration visual = VisualDecoration.Public, CallingDecoration calling = CallingDecoration.Standard)
     : base(visual, calling)
 {
     Context = new BuildContext(context)
     {
         PropertyBuilder = this
     };
     Name         = propertyName;
     PropertyType = propertyType;
     InitBuilder();
 }