【问题标题】:C# Reflection: How do I initialize a field created dynamically in TypeBuilder?C# 反射:如何初始化在 TypeBuilder 中动态创建的字段?
【发布时间】:2011-12-07 16:38:44
【问题描述】:

我正在创建一个动态 dll 来保存从我的数据库创建的自定义对象。我可以按照我想要的方式创建字段,但是我不明白如何调用构造函数。对于最终生成的结果,我想要:

public class Countries
{
    public Countries() { }
    public static readonly ReferenceObject USA = new ReferenceObject(120);
    public static readonly ReferenceObject CAN = new ReferenceObject(13);
    public static readonly ReferenceObject MEX = new ReferenceObject(65);
    ... //These would be populated from the database
}

我得到的是

public class Countries
{
    public Countries() { }
    public static readonly ReferenceObject USA;
    public static readonly ReferenceObject CAN;
    public static readonly ReferenceObject MEX;
    ...
}

如何将值设置为新的初始化对象?

AppDomain domain = AppDomain.CurrentDomain;

AssemblyName aName = new AssemblyName("DynamicEnums");
AssemblyBuilder ab = domain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);

ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

foreach(ReferenceType rt in GetTypes())
{
    TypeBuilder tb = mb.DefineType(rt.Name, TypeAttributes.Public);

    foreach (Reference r in GetReferences(rt.ID))
    {
        string name = NameFix(r.Name);

        FieldBuilder fb = tb.DefineField(name, typeof(ReferenceObject), FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.Literal);

        //Call constructor here... how???
    }

    types.Add(tb.CreateType());
}

ab.Save(aName.Name + ".dll");

【问题讨论】:

  • 也许我想在这里将它们切换到 DefineMethod 或 DefineProperty...但最终结果是我希望 ("120" == countries.USA) 以及 (Countries. USA == 120) 在我的代码中引用时为真。
  • ReferenceObject 基本上是一个 int 的包装器,允许将其与 int 或字符串进行比较。

标签: c# .net reflection dll dynamic


【解决方案1】:

从我的answer 到您的其他(非常相似的)问题的一点复制面食,但是:

AppDomain domain = AppDomain.CurrentDomain;

AssemblyName aName = new AssemblyName("DynamicEnums");
AssemblyBuilder ab = domain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Save);

ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

// Store a handle to the ReferenceObject(int32 pValue)
// constructor.
ConstructorInfo referenceObjectConstructor = typeof(ReferenceObject).GetConstructor(new[] { typeof(int) });

foreach (ReferenceType rt in GetTypes())
{
    TypeBuilder tb = mb.DefineType(rt.Name, TypeAttributes.Public);

    // Define a static constructor to populate the ReferenceObject
    // fields.
    ConstructorBuilder staticConstructorBuilder = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
    ILGenerator staticConstructorILGenerator = staticConstructorBuilder.GetILGenerator();

    foreach (Reference r in GetReferences(rt.ID))
    {
        string name = r.Abbreviation.Trim();

        // Create a public, static, readonly field to store the
        // named ReferenceObject.
        FieldBuilder referenceObjectField = tb.DefineField(name, typeof(ReferenceObject), FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.InitOnly);

        // Add code to the static constructor to populate the
        // ReferenceObject field:

        // Load the ReferenceObject's ID value onto the stack as a
        // literal 4-byte integer (Int32).
        staticConstructorILGenerator.Emit(OpCodes.Ldc_I4, r.ID);

        // Create a reference to a new ReferenceObject on the stack
        // by calling the ReferenceObject(int32 pValue) reference
        // we created earlier.
        staticConstructorILGenerator.Emit(OpCodes.Newobj, referenceObjectConstructor);

        // Store the ReferenceObject reference to the static
        // ReferenceObject field.
        staticConstructorILGenerator.Emit(OpCodes.Stsfld, referenceObjectField);
    }

    // Finish the static constructor.
    staticConstructorILGenerator.Emit(OpCodes.Ret);

    tb.CreateType();
}

ab.Save(aName.Name + ".dll");

【讨论】:

    【解决方案2】:

    当编写一个 C# 类时,如您的示例中那样初始化字段,编译器所做的是在构造函数中生成初始化代码。

    我认为你应该这样做:使用TypeBuilder.DefineConstructor 生成构造函数,并在内部创建代码来设置字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2011-08-26
      • 2015-07-10
      • 2019-09-06
      相关资源
      最近更新 更多