There is a demand in the work, there are n different reports, each report corresponding to a data source, before the use of SQL statistics, statistical data using memory mode, first within the definition of the data source corresponding entity. Statistical conditions are implemented using the LAMDBA expression, which defines the cell by means of a tool. In the implementation process for each table to write the number display is very low, take a few pieces of the definition is a rule, statistical implementation is a C # code, but to different data sources is different list<entity>, through the research found through generics and reflection can be achieved.
Get entity types based on string
1 PrivateType GetEntity (stringtypeName)2 {3 varWorkpath =AppDomain.CurrentDomain.BaseDirectory;4 string[] files = Directory.GetFiles (Workpath,"XXX. Utils.dll", searchoption.topdirectoryonly);5 foreach(stringFileinchfiles)6 {7 stringext = file. Substring (file. LastIndexOf ("."));8 if(Ext! =". DLL")Continue;9 TryTen { OneAssembly asm =assembly.loadfile (file); Atype[] Alltypes =ASM. GetTypes (); - foreach(Type tinchalltypes) - { the if(T.issubclassof (typeof(Xxdatarecord))) - { - if(T.name.toupper (). Issame (Typename.toupper ())) - returnT; + } - } + } A Catch at { - return NULL; - } - } - return NULL; -}
Generic method definition
1 Public string where New ()
Calling method code
1 varobj =NewGeneratedatahelper (_param);2Type Mygenericclasstype =obj. GetType ();3 //makegenericmethod Setting generic parameter types4MethodInfo mi = Mygenericclasstype.getmethod ("Builddatamemorypattern"). MakeGenericMethod (typeentity);5 intCount =0;6 //Set Call method parameters7 Object[] Invokeargs =New Object[] {ZB, ID, count};8 //called9 varDT =(DataTable) mi. Invoke (obj, Invokeargs);Ten //Get Return Data OneRecordCount = (int) invokeargs[9];
The following is all the code, the specific function implementation skipped
1 Public classUtilfetchhelper2 {3 //Get entity type4 PrivateType GetEntity (stringtypeName)5 {6 varWorkpath =AppDomain.CurrentDomain.BaseDirectory;7 string[] files = Directory.GetFiles (Workpath,"XXX. Utils.dll", searchoption.topdirectoryonly);8 foreach(stringFileinchfiles)9 {Ten stringext = file. Substring (file. LastIndexOf (".")); One if(Ext! =". DLL")Continue; A Try - { -Assembly asm =assembly.loadfile (file); thetype[] Alltypes =ASM. GetTypes (); - foreach(Type tinchalltypes) - { - if(T.issubclassof (typeof(Xxdatarecord))) + { - if(T.name.toupper (). Issame (Typename.toupper ())) + returnT; A } at } - } - Catch - { - return NULL; - } in } - return NULL; to } + - PublicDataTable Builddatamemorypattern (Xxparams ZB,stringId out intrecordCount) the { * //get entity names based on incoming parameters $ varEntityName =Getdatasourceentityname (ZB);Panax Notoginseng //Get entity type -Type typeentity =getentity (entityname); the if(Typeentity = =NULL) + return NULL; A varobj =NewGeneratedatahelper (_param); theType Mygenericclasstype =obj. GetType (); +MethodInfo mi = Mygenericclasstype.getmethod ("Builddatamemorypattern"). MakeGenericMethod (typeentity); - intCount =0; $ Object[] Invokeargs =New Object[] {ZB, ID, count}; $ varDT =(DataTable) mi. Invoke (obj, Invokeargs); - //get Return Data here -RecordCount = (int) invokeargs[9]; the returnDT; - }Wuyi } the - Public classGeneratedatahelper Wu { - PrivateList<xxdatarecord>_datalist; About PrivateList<xxdatarecord>DataList $ { - Set{_datalist =value;} - Get{return_datalist;} - } A + PrivateList<t> getdatasource<t> ()whereT:xxdatarecord,New() the { - var_datalist =NewList<t>(); $ varDT = DB. Executedatatable ("Select * from XX"); the for(inti =0; i < dt. Rows.Count; i++) the { the varDataent =NewT (); the for(intj =0; J < dt. Columns.count; J + +) - { inDataent[dt. COLUMNS[J]. ColumnName] =dt. ROWS[I][J]; the } the _datalist.add (dataent); About } the return_datalist; the } the + - PublicXxdatamemory builddatamemorypattern<t> (Xxparams ZB,stringIdwhereT:xxdatarecord,New() the {BayiList<t> DataList =NewList<t>(); theDataList = getdatasource<t>(); the //The following is a specific implementation code for DataList query Operations - } -}
View Code
C # reflects a string into an entity class and is used as a parameter in an incoming generic method