1

ClosedDt という名前の古い余分なプロパティのすべてのプロパティをextension method返すものを書き込もうとしています。私は今これを手に入れました:new typetype +

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 fieldsnew typeます。何が欠けているのか、どこを間違えたのか?

4

1 に答える 1