我正在尝试编写extension
method
将返回名为 ClosedDtnew
type
的旧额外属性的所有属性。type
+
我现在得到了这个:
public static object GetDynamicObject(this System.Reflection.Emit.TypeBuilder typeBuilder,AssemblyName assembly, Type objectType)
{
AppDomain appDomain = System.Threading.Thread.GetDomain();
AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(assembly, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assembly.Name);
//create the class
typeBuilder = moduleBuilder.DefineType(objectType.Name, TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit, typeof(System.Object));
foreach (var prop in objectType.GetProperties())
{
FieldBuilder fieldBuilder = typeBuilder.DefineField(prop.Name.Substring(0, 1).ToLower() + prop.Name.Substring(1),
prop.PropertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(prop.Name, PropertyAttributes.None,
prop.PropertyType, new Type[]{prop.PropertyType});
MethodBuilder propertyGetter = typeBuilder.DefineMethod("get_" + prop.Name, MethodAttributes.Public | MethodAttributes.HideBySig, prop.PropertyType, new Type[] { prop.PropertyType });
var ilGenerator = propertyGetter.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld,fieldBuilder);
ilGenerator.Emit(OpCodes.Ret);
MethodBuilder propertySetter = typeBuilder.DefineMethod("set_"+prop.Name, MethodAttributes.Public | MethodAttributes.HideBySig, prop.PropertyType, new Type[] { prop.PropertyType });
var propertySetterIl = propertySetter.GetILGenerator();
propertySetterIl.Emit(OpCodes.Ldarg_0);
propertySetterIl.Emit(OpCodes.Ldarg_1);
propertySetterIl.Emit(OpCodes.Stfld, fieldBuilder);
propertySetterIl.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(propertyGetter);
propertyBuilder.SetGetMethod(propertySetter);
}
FieldBuilder closedFieldBuilder = typeBuilder.DefineField("closedDt",
typeof(string), FieldAttributes.Private);
PropertyBuilder closedPropertyBuilder = typeBuilder.DefineProperty("ClosedDt", PropertyAttributes.HasDefault,
typeof(string), null);
MethodBuilder closedPropertyGetter = typeBuilder.DefineMethod("get_ClosedDt", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, typeof(string), Type.EmptyTypes);
var ilGeneratorClosed = closedPropertyGetter.GetILGenerator();
ilGeneratorClosed.Emit(OpCodes.Ldarg_0);
ilGeneratorClosed.Emit(OpCodes.Ldfld, closedFieldBuilder);
ilGeneratorClosed.Emit(OpCodes.Ret);
MethodBuilder closedPropertySetter = typeBuilder.DefineMethod("set_ClosedDt", MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig, null, new Type[] { typeof(string) });
var closedPropertySetterIl = closedPropertySetter.GetILGenerator();
closedPropertySetterIl.Emit(OpCodes.Ldarg_0);
closedPropertySetterIl.Emit(OpCodes.Ldarg_1);
closedPropertySetterIl.Emit(OpCodes.Stfld, closedFieldBuilder);
closedPropertySetterIl.Emit(OpCodes.Ret);
closedPropertyBuilder.SetGetMethod(closedPropertyGetter);
closedPropertyBuilder.SetGetMethod(closedPropertySetter);
var dynamicType = typeBuilder.CreateType();
return Activator.CreateInstance(dynamicType);
}
作为记录:我知道如果我还想向属性添加一些属性,我需要从头开始创建新类。
但上面的代码只private
fields
为new
type
. 我错过了什么或我犯了什么错误?