C# 生成IL UPCODE以将值插入字典

C# 生成IL UPCODE以将值插入字典,c#,.net,C#,.net,给定以下类 public class OgrEntity { public OgrEntity() { Properties = new Dictionary<string, object>(); } public DbGeometry ogr_geometry { get; set; } public int ogr_fid { get; set; } public Dictionary<string, ob

给定以下类

public class OgrEntity 
{
    public OgrEntity()
    {
        Properties = new Dictionary<string, object>();
    }
    public DbGeometry ogr_geometry { get; set; }
    public int ogr_fid { get; set; }

    public Dictionary<string, object> Properties;

}
public-class-ogrenty
{
公众舆论()
{
属性=新字典();
}
公共DbGeometry ogr_geometry{get;set;}
public int ogr_fid{get;set;}
公共字典属性;
}
我试图在上面的类中创建一个类的动态属性作为它的基础

    public static TypeBuilder CreateTypeBuilder<T>(
        string assemblyName, string moduleName, string typeName)
    {
        TypeBuilder typeBuilder = AppDomain
            .CurrentDomain
            .DefineDynamicAssembly(new AssemblyName(assemblyName),
                                   AssemblyBuilderAccess.Run)
            .DefineDynamicModule(moduleName)
            .DefineType(typeName, TypeAttributes.Public,typeof(T));
        typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

        return typeBuilder;
    }
    public static void CreateDictionaryWrappingProperty(TypeBuilder builder, string propertyName, Type propertyType)
    {

        const string SetterPrefix = "set_";

        // Dictionary to store.
        var dictType = typeof(Dictionary<string, object>);
        var dict = typeof(OgrEntity).GetProperty("Properties");
        var addMethod = dictType.GetMethod("Add");

        // Generate the property
        PropertyBuilder propertyBuilder = builder.DefineProperty(
            propertyName, PropertyAttributes.HasDefault, propertyType, null);

        // Property getter and setter attributes.
        MethodAttributes propertyMethodAttributes =
            MethodAttributes.Public | MethodAttributes.SpecialName |
            MethodAttributes.HideBySig;

        // Define the setter method.
        MethodBuilder setterMethod = builder.DefineMethod(
            string.Concat(SetterPrefix, propertyName),
            propertyMethodAttributes, null, new Type[] { propertyType });

        ILGenerator setterILCode = setterMethod.GetILGenerator();

       //What IL code do i need to do the following method:
       //set{ this.Properties.Add(propertyName,value); }

       //What IL code is need to do the getter
       //get{ if(this.Properties.ContainsKey(propertyName))
       //           return this.Properties[propertyName] //Cast to right type also;
       //       return null;
       //  }


        propertyBuilder.SetSetMethod(setterMethod);
    }
公共静态类型生成器CreateTypeBuilder(
字符串组合名、字符串模块名、字符串类型名)
{
TypeBuilder TypeBuilder=AppDomain
.CurrentDomain
.DefinedDynamicAssembly(新的AssemblyName(AssemblyName)),
AssemblyBuilderAccess.Run)
.DefinedDynamicModule(模块名称)
.DefineType(typeName,TypeAttributes.Public,typeof(T));
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
返回类型生成器;
}
公共静态void CreateDictionaryRappingProperty(TypeBuilder生成器、字符串propertyName、类型propertyType)
{
常量字符串SetterPrefix=“set_u2;”;
//要储存的字典。
var DICTYPE=类型(字典);
var dict=typeof(OgrEntity).GetProperty(“属性”);
var addMethod=dictType.GetMethod(“添加”);
//生成属性
PropertyBuilder PropertyBuilder=builder.DefineProperty(
propertyName,PropertyAttributes.HasDefault,propertyType,null);
//属性getter和setter属性。
MethodAttributes属性MethodAttributes=
MethodAttributes.Public | MethodAttributes.SpecialName|
MethodAttributes.hidebysing;
//定义setter方法。
MethodBuilder setterMethod=builder.DefineMethod(
Concat(SetterPrefix,propertyName),
PropertyMethodAttribute,null,新类型[]{propertyType});
ILGenerator setterCode=setterMethod.GetILGenerator();
//我需要什么IL代码来执行以下方法:
//设置{this.Properties.Add(propertyName,value);}
//做getter需要什么IL代码
//获取{if(this.Properties.ContainsKey(propertyName))
//返回此.Properties[propertyName]//也转换为右类型;
//返回null;
//  }
propertyBuilder.SetSetMethod(setterMethod);
}
测试:

TypeBuilder=Program.CreateTypeBuilder(
“MyDynamicsSembly”、“MyModule”、“MyType”);
createDictionaryRappingProperty(builder,“uuid”,typeof(string));
类型resultType=builder.CreateType();
编译你想要的C代码。反汇编生成的二进制文件。这将教会您需要发出什么操作码

TypeBuilder builder = Program.CreateTypeBuilder<OgrEntity>(
            "MyDynamicAssembly", "MyModule", "MyType");
Program.CreateDictionaryWrappingProperty(builder, "uuid", typeof(string));
Type resultType = builder.CreateType();