gpt4 book ai didi

c# - 如何在运行时只在类上添加一个属性?

转载 作者:行者123 更新时间:2023-11-30 21:01:04 27 4
gpt4 key购买 nike

我设法在运行时使用 Reflection Emit 创建了这个类:

[DelimitedRecord(",")]
public partial 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
Type[] delimiterAttributeParams = new Type[] { typeof(string) };
ConstructorInfo delimiterAttrInfo = typeof(DelimitedRecordAttribute).GetConstructor(delimiterAttributeParams);
CustomAttributeBuilder delimiterAttributeBuilder = new CustomAttributeBuilder(delimiterAttrInfo, new object[] { ";" });
typeBuilder.SetCustomAttribute(delimiterAttributeBuilder);

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

//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);

//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)]
Type[] lastNameFieldOrderAttributeParams = new Type[] { typeof(int) };
ConstructorInfo lastNameFieldOrderAttrInfo = typeof(FieldOrderAttribute).GetConstructor(lastNameFieldOrderAttributeParams);
CustomAttributeBuilder lastNameFieldOrderAttributeBuilder = new CustomAttributeBuilder(lastNameFieldOrderAttrInfo, new object[] { 1 });
lastNameField.SetCustomAttribute(lastNameFieldOrderAttributeBuilder);

//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);

我真正想要的是像这样声明我的类:

 public partial class Person
{
private string firstName;

private string lastName;

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

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

并且在运行时只添加那些属性。对于名字字段,我正在考虑这样的事情

    //get the builder to that field
FieldBuilder firstNameField =

//create the 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);

如何让 build 者到达那个领域?

最佳答案

正如 rpgmaker 在评论中所说的那样,类在加载后无法修改。有一些解决方法:

  1. 使用 Mono Cecil 修改现有程序集。
  2. 使用 PostSharp 之类的东西作为构建后步骤,将属性添加到程序集。
  3. 在运行时创建一个新类,它基本上是添加了属性的类的副本。
  4. 在实际编译之前使用 Roslyn 之类的工具修改源代码。

但所有这些都只是解决方法。而且您的最终目标实际上并不是向文件添加属性,它似乎是使用 FileHelpers 读取 CSV 文件而无需指定必要的属性。

There is a page on the FileHelpers site describing how to do that ,使用几种不同的方法,包括从 XML 加载格式。

关于c# - 如何在运行时只在类上添加一个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483523/

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