定义一个数据模型类,成员对应数据表中的字段。要求在运行时增加成员,最后返回该数据模型对像?如表结构:(对应数据库表)
public int id{get;set;}
public int AppId { get; set; }
public string TableName { get; set; }
public string FieldJSON { get; set; } //表的字段信息JSON数据public bool SaveAppProperty(string AppId, string dataJSON)
{
Model.AppPropertyTable tbInfo = this.GetAppPropertyTableInfo(AppId);
if (tbInfo != null)
{
string PropertyTable = tbInfo.TableName;
问题在这里:
//dataJSON字段结构是未知的,object这个应为动态的数据模型对像,,object不知能否动态创建(或先创建后,动态增加成员)???????? object PropertyObj = KstUtilities.JsonUtility.DecodeObject<object>(dataJSON);
return IDataBase.DbCompoment.ExecuteUpdate(PropertyTable,"RoleId=0
}
else
{
throw new OAbase.OAException("未能找到应用程序属性表!");
}
}
public int id{get;set;}
public int AppId { get; set; }
public string TableName { get; set; }
public string FieldJSON { get; set; } //表的字段信息JSON数据public bool SaveAppProperty(string AppId, string dataJSON)
{
Model.AppPropertyTable tbInfo = this.GetAppPropertyTableInfo(AppId);
if (tbInfo != null)
{
string PropertyTable = tbInfo.TableName;
问题在这里:
//dataJSON字段结构是未知的,object这个应为动态的数据模型对像,,object不知能否动态创建(或先创建后,动态增加成员)???????? object PropertyObj = KstUtilities.JsonUtility.DecodeObject<object>(dataJSON);
return IDataBase.DbCompoment.ExecuteUpdate(PropertyTable,"RoleId=0
}
else
{
throw new OAbase.OAException("未能找到应用程序属性表!");
}
}
你好,用IL Emit 如下面这样一个类,怎么动态创建,谢谢!
public className:model.dsModel{
public id{get;set;}
public name{get;set;}
}像下面我参考了一些IL Emit 写的方法,不知是否正确,还有不知怎么去实例化这个类。
protected virtual Type CreateDynamicClass(List<Model.FieldStruct> FieldList,string TableName)
{
var asmName = new AssemblyName(TableName);
var asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.ReflectionOnly);
var asmModelBuilder = asmBuilder.DefineDynamicModule(TableName, TableName + ".dll");
var asmTypeBuilder = asmModelBuilder.DefineType(TableName, TypeAttributes.Public, typeof(Model.ModelBs));
foreach (Model.FieldStruct Field in FieldList)
{
var TypeGetMethod = asmTypeBuilder.DefineMethod("Get" + Field.FieldName, MethodAttributes.Public, this.GetFieldDataTypeForCshrp(Field.DataType), null);
var TypeSetMethod = asmTypeBuilder.DefineMethod("Set" + Field.FieldName, MethodAttributes.Public, null, new Type[] { this.GetFieldDataTypeForCshrp(Field.DataType) });
var ilGet = TypeGetMethod.GetILGenerator();
ilGet.Emit(OpCodes.Ldarg_0);
ilGet.Emit(OpCodes.Ldfld);
ilGet.Emit(OpCodes.Ret);
var ilSet = TypeSetMethod.GetILGenerator();
ilSet.Emit(OpCodes.Ldarg_0);
ilSet.Emit(OpCodes.Ldarg_1);
ilSet.Emit(OpCodes.Stfld);
var prpt = asmTypeBuilder.DefineProperty(Field.FieldName, System.Reflection.PropertyAttributes.None, this.GetFieldDataTypeForCshrp(Field.DataType), null);
prpt.SetGetMethod(TypeGetMethod);
prpt.SetSetMethod(TypeSetMethod);
}
Type objType=asmTypeBuilder.CreateType();
asmBuilder.Save(TableName + ".dll");
return objType;
}