gpt4 book ai didi

c# - 使用 Reflection.Emit 动态创建一个类。我被困

转载 作者:太空狗 更新时间:2023-10-29 17:56:24 27 4
gpt4 key购买 nike

读了一篇关于在运行时创建类型的文章,我发现它很神奇。我的目标是创建这个类:

[DelimitedRecord(",")]
public class Person
{
[FieldOrder(0)]
private string firstName;

[FieldOrder(1)]
private string lastName;

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}

我这样做了:

//create the builder
AssemblyName assembly = new AssemblyName("FileHelpersTests");
AppDomain appDomain = System.Threading.Thread.GetDomain();
AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(assembly, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assembly.Name);

//create the class
TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit, typeof(System.Object));

//create the Delimiter attribute

//create the firstName field
FieldBuilder firstNameField = typeBuilder.DefineField("firstName", typeof(System.String), FieldAttributes.Private);

//create the firstName attribute [FieldOrder(0)]

//create the FirstName property
PropertyBuilder firstNameProperty = typeBuilder.DefineProperty("FirstName", PropertyAttributes.HasDefault, typeof(System.String), null);

//create the FirstName Getter
MethodBuilder firstNamePropertyGetter = typeBuilder.DefineMethod("get_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, typeof(System.String), Type.EmptyTypes);
ILGenerator firstNamePropertyGetterIL = firstNamePropertyGetter.GetILGenerator();
firstNamePropertyGetterIL.Emit(OpCodes.Ldarg_0);
firstNamePropertyGetterIL.Emit(OpCodes.Ldfld, firstNameField);
firstNamePropertyGetterIL.Emit(OpCodes.Ret);

//create the FirstName Setter
MethodBuilder firstNamePropertySetter = typeBuilder.DefineMethod("set_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, null, new Type[] { typeof(System.String) });
ILGenerator firstNamePropertySetterIL = firstNamePropertySetter.GetILGenerator();
firstNamePropertySetterIL.Emit(OpCodes.Ldarg_0);
firstNamePropertySetterIL.Emit(OpCodes.Ldarg_1);
firstNamePropertySetterIL.Emit(OpCodes.Stfld, firstNameField);
firstNamePropertySetterIL.Emit(OpCodes.Ret);

//assign getter and setter
firstNameProperty.SetGetMethod(firstNamePropertyGetter);
firstNameProperty.SetSetMethod(firstNamePropertySetter);


//create the lastName field
FieldBuilder lastNameField = typeBuilder.DefineField("lastName", typeof(System.String), FieldAttributes.Private);

//create the lastName attribute [FieldOrder(1)]

//create the LastName property
PropertyBuilder lastNameProperty = typeBuilder.DefineProperty("LastName", PropertyAttributes.HasDefault, typeof(System.String), null);

//create the LastName Getter
MethodBuilder lastNamePropertyGetter = typeBuilder.DefineMethod("get_LastName", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, typeof(System.String), Type.EmptyTypes);
ILGenerator lastNamePropertyGetterIL = lastNamePropertyGetter.GetILGenerator();
lastNamePropertyGetterIL.Emit(OpCodes.Ldarg_0);
lastNamePropertyGetterIL.Emit(OpCodes.Ldfld, lastNameField);
lastNamePropertyGetterIL.Emit(OpCodes.Ret);

//create the FirstName Setter
MethodBuilder lastNamePropertySetter = typeBuilder.DefineMethod("set_FirstName", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, null, new Type[] { typeof(System.String) });
ILGenerator lastNamePropertySetterIL = lastNamePropertySetter.GetILGenerator();
lastNamePropertySetterIL.Emit(OpCodes.Ldarg_0);
lastNamePropertySetterIL.Emit(OpCodes.Ldarg_1);
lastNamePropertySetterIL.Emit(OpCodes.Stfld, lastNameField);
lastNamePropertySetterIL.Emit(OpCodes.Ret);

//assign getter and setter
lastNameProperty.SetGetMethod(lastNamePropertyGetter);
lastNameProperty.SetSetMethod(lastNamePropertySetter);

我在创建这些属性时卡住了:

  • 类(class)水平
  • 现场级的

这是我点击时 ILDASM 显示的内容

  1. 构造函数

    .class public auto ansi beforefieldinit FileHelpersTests.Person
    extends [mscorlib]System.Object
    {
    .custom instance void [FileHelpers]FileHelpers.DelimitedRecordAttribute::.ctor(string) = ( 01 00 01 2C 00 00 ) // ...,..
    } // end of class FileHelpersTests.Person
  2. 名字字段

    .field private string firstName
    .custom instance void [FileHelpers]FileHelpers.FieldOrderAttribute::.ctor(int32) = ( 01 00 00 00 00 00 00 00 )

最佳答案

我成功了:

//create the Delimiter attribute
Type[] attributeParams = new Type[] { typeof(string) };
ConstructorInfo classCtorInfo = typeof(DelimitedRecordAttribute).GetConstructor(attributeParams);
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(classCtorInfo, new object[] { ";" });
typeBuilder.SetCustomAttribute(attributeBuilder);

这将完成字段属性的工作:

//create the firstName attribute [FieldOrder(0)]
Type[] firstNameFieldOrderAttributeParams = new Type[] { typeof(int) };
ConstructorInfo firstNameFieldOrderAttrInfo = typeof(FieldOrderAttribute).GetConstructor(firstNameFieldOrderAttributeParams);
CustomAttributeBuilder firstNameFieldOrderAttributeBuilder = new CustomAttributeBuilder(firstNameFieldOrderAttrInfo, new object[] { 0 });
firstNameField.SetCustomAttribute(firstNameFieldOrderAttributeBuilder);

关于c# - 使用 Reflection.Emit 动态创建一个类。我被困,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14480332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com