In MVVM development mode, the GridView In The xaml file corresponds to an object set ObserverCollection <T> In Viewmodel, which is not explained much, the attributes in the object T may be dynamic, for example, using CategoryName as the column name. In this case, we need to dynamically create a class where the attributes are extracted from the database. Let's take a look at the implementation of this generation tool, some of which are not frequently used.
Public class DynamicTypeTools {// <summary> // Attribute class // </summary> public class DynamicDetail {// <summary> // attribute data type // /</summary> public Type PropertyType {get; set ;}//< summary> /// attribute name /// </summary> public string PropertyName {get; set ;}} /// <summary> /// dynamically create a class /// </summary> /// <param name = "list"> set of attribute classes </param> /// <returns> created class </returns> public static Type CreateClass (List <DynamicDetail> list) {TypeBuilder tb = GetTypeBuilder (list. getHashCode (); ConstructorBuilder constructor = tb. definedefaconconstructor (MethodAttributes. public | MethodAttributes. specialName | MethodAttributes. RTSpecialName); foreach (var item in list) {CreateProperty (tb, item. propertyName, item. propertyType);} return tb. createType ();} private static TypeBuilder GetTypeBuilder (int code) {AssemblyName an = new AssemblyName ("TempAssembly" + code); AssemblyBuilder assemblyBuilder = AppDomain. currentDomain. defineDynamicAssembly (,
AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule"); TypeBuilder tb = moduleBuilder.DefineType("TempType" + code , TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout , typeof(object)); return tb; } private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType) { FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private); PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName,
PropertyAttributes.HasDefault, propertyType, null); MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes); ILGenerator getIL = getPropMthdBldr.GetILGenerator(); getIL.Emit(OpCodes.Ldarg_0); getIL.Emit(OpCodes.Ldfld, fieldBuilder); getIL.Emit(OpCodes.Ret); MethodBuilder setPropMthdBldr = tb.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new Type[] { propertyType }); ILGenerator setIL = setPropMthdBldr.GetILGenerator(); setIL.Emit(OpCodes.Ldarg_0); setIL.Emit(OpCodes.Ldarg_1); setIL.Emit(OpCodes.Stfld, fieldBuilder); setIL.Emit(OpCodes.Ret); propertyBuilder.SetGetMethod(getPropMthdBldr); propertyBuilder.SetSetMethod(setPropMthdBldr); } }
This class has a static method and a Model class of an attribute. This section briefly introduces the Type CreateClass (List <DynamicDetail> list) method, DynamicDetail
There are two attributes in the class. One is the attribute name of the dynamic class and the other is the attribute type of the dynamic class. when creating the dynamic class, you only need to pass a DynamicDetail to the CreateClass method.
To return a created class. The usage is as follows:
:http://files.cnblogs.com/wengyuli/DynamicTypeTools.rar