Share a dynamic class generation tool from a colleague

Source: Internet
Author: User

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
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.