动态地将属性添加到属性

本文关键字:属性 添加 动态 | 更新日期: 2023-09-27 18:35:43

以下实现,它动态创建类和属性:

public static class MyTypeBuilder
{
    public static void CreateNewObject()
    {
        var myType = CompileResultType();
    }
    public static Type CompileResultType()
    {
        TypeBuilder tb = GetTypeBuilder();
        ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
                        var fields = new List<Field>() { 
            new Field("EmployeeID", typeof(int)),
            new Field("EmployeeName", typeof(string)),
            new Field("Designation", typeof(string)) 
        };
        // NOTE: assuming your list contains Field objects with fields FieldName(string) and FieldType(Type)
                        foreach (var field in fields)
            CreateProperty(tb, field.FieldName, field.FieldType);
        Type objectType = tb.CreateType();
        return objectType;
    }
    private static TypeBuilder GetTypeBuilder()
    {
        var typeSignature = "MyDynamicType";
        var an = new AssemblyName(typeSignature);
        AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
        TypeBuilder tb = moduleBuilder.DefineType(typeSignature
                            , TypeAttributes.Public |
                            TypeAttributes.Class |
                            TypeAttributes.AutoClass |
                            TypeAttributes.AnsiClass |
                            TypeAttributes.BeforeFieldInit |
                            TypeAttributes.AutoLayout
                            , null);
        return tb;
    }
    private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType)
    {
        FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
        PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
        MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes);
        ILGenerator getIl = getPropMthdBldr.GetILGenerator();
        getIl.Emit(OpCodes.Ldarg_0);
        getIl.Emit(OpCodes.Ldfld, fieldBuilder);
        getIl.Emit(OpCodes.Ret);
        MethodBuilder setPropMthdBldr =
            tb.DefineMethod("set_" + propertyName,
              MethodAttributes.Public |
              MethodAttributes.SpecialName |
              MethodAttributes.HideBySig,
              null, new[] { propertyType });
        ILGenerator setIl = setPropMthdBldr.GetILGenerator();
        Label modifyProperty = setIl.DefineLabel();
        Label exitSet = setIl.DefineLabel();
        setIl.MarkLabel(modifyProperty);
        setIl.Emit(OpCodes.Ldarg_0);
        setIl.Emit(OpCodes.Ldarg_1);
        setIl.Emit(OpCodes.Stfld, fieldBuilder);
        setIl.Emit(OpCodes.Nop);
        setIl.MarkLabel(exitSet);
        setIl.Emit(OpCodes.Ret);
        propertyBuilder.SetGetMethod(getPropMthdBldr);
        propertyBuilder.SetSetMethod(setPropMthdBldr);
    }
}

在上面的相同实现中,我想为属性添加属性例

[DelimitedRecord(",")]
   [IgnoreEmptyLines()]
   private class myDyanmicType
   {   
       [FieldQuoted('"', QuoteMode.OptionalForBoth)]
       public int EmployeeID;     
       [FieldQuoted('"', QuoteMode.OptionalForBoth)]
       public String EmployeeName;
       [FieldQuoted('"', QuoteMode.OptionalForBoth)]
       public String Designation;          
   }

所以我想为属性添加属性,例如

[FieldQuoted('"', QuoteMode.OptionalForBoth)]

我将如何使用创建动态类和属性的代码来实现这一点

请帮忙。

动态地将属性添加到属性

只是一种更简单、更快捷的方法:将您欣赏的代码编写为字符串,然后在运行时编译它,您将准备好一切!

string src = "[DelimitedRecord('",'")] [IgnoreEmptyLines()] private class ...";
var compParms = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory = true
            };
var csProvider = new CSharpCodeProvider();
CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, src);
var a = compilerResults.CompiledAssembly; // here you go