C # code for converting a able into an object set using delegate reflection

Source: Internet
Author: User

Generic constraints: Copy codeThe Code is as follows: public static class ToModel <T> where T: class, new ()

Define delegation:Copy codeThe Code is as follows: public delegate void SetString (string value );

Delegate creation method:Copy codeThe Code is as follows: private static SetString CreateStringDelegate (T model, string propertyName)
{
MethodInfo mi = model. GetType (). GetProperty (propertyName). GetSetMethod ();
Type type = typeof (SetString );
Return Delegate. CreateDelegate (type, model, mi) as SetString;
}

Use reflection and delegation to convert a able to an object set:Copy codeThe Code is as follows: public static IList <T> GetDelegate_ToModelList (DataTable dt)
{
IList <T> list = new List <T> ();
If (dt = null | dt. Rows. Count <1) return list;
SetString setDelegateString;
Foreach (DataRow dr in dt. Rows)
{
T model = new T ();
Foreach (DataColumn dc in dt. Columns)
{
SetDelegateString = CreateStringDelegate (model, dc. ColumnName );
SetDelegateString (dr [dc. ColumnName]. ToString ());
}
List. Add (model );
}
Return list;
}

In this case, the write problem arises, because the parameter defined by the delegate is of the string type, because we can have the int or DateTime type in the entity, then we need to use the generic delegate.
If the delegate is defined as follows:Copy codeThe Code is as follows: public delegate void SetString <PT> (PT value)

Create the delegate method (here there is a problem and I do not know how to handle it ):Copy codeThe Code is as follows: private static SetString CreateStringDelegate (T model, string propertyName)
{
MethodInfo mi = model. GetType (). GetProperty (propertyName). GetSetMethod ();
Type type = typeof (model). GetProperty (propertyName). PropertyType;
Return Delegate. CreateDelegate (type, model, mi) as SetString <type>;
}

Use reflection and delegation to convert a able to an object set:Copy codeThe Code is as follows: public static IList <T> GetDelegate_ToModelList (DataTable dt)
{
IList <T> list = new List <T> ();
If (dt = null | dt. Rows. Count <1) return list;
Foreach (DataRow dr in dt. Rows)
{
T model = new T ();
Foreach (DataColumn dc in dt. Columns)
{
SetString <typeof (T). GetProperty (dc. ColumnName). PropertyType> setDelegateString = CreateStringDelegate (model, dc. ColumnName );
SetDelegateString (dr [dc. ColumnName]. ToString ());
}
List. Add (model );
}
Return list;
}

I have been wondering. I hope someone can help me solve this problem. I also have some direct reflection methods, but this problem cannot be solved. I have always had a flaw in my mind. I hope someone can help me. Thank you.
The generic model can be dynamically built. After you understand this, you can solve the problem by attaching my simple code:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. Reflection;
Namespace RftToModel {
Class Program {
Static void Main (string [] args ){
Var result = ToModel <TestModel>. GetDelegate_ToModelList (BuildSampleTable ());
Foreach (var item in result ){
Console. WriteLine (item );
}
Console. Read ();
}
Static DataTable BuildSampleTable (){
DataTable result = new DataTable ();
Result. Columns. Add ("ID", typeof (int ));
Result. Columns. Add ("Name", typeof (string ));
Result. Columns. Add ("IsDeleted", typeof (bool ));
Result. Rows. Add (new object [] {1, "M. K", false });
Result. Rows. Add (new object [] {2, "B .G", true });
Return result;
}
}
Public class TestModel {
Public int ID {get; set ;}
Public string Name {get; set ;}
Public bool IsDeleted {get; set ;}
Public override string ToString (){
Return string. Format ("ID: {0} Name: {1} IsDeleted: {2}", ID, Name, IsDeleted );
}
}
Public delegate void SetValue <T> (T value );
Public static class ToModel <T> where T: class, new (){
Private static Delegate CreateSetDelegate (T model, string propertyName ){
MethodInfo mi = model. GetType (). GetProperty (propertyName). GetSetMethod ();
// Construct the generic delegate type here
Type delType = typeof (SetValue <>). MakeGenericType (GetPropertyType (propertyName ));
Return Delegate. CreateDelegate (delType, model, mi );
}
Private static Type GetPropertyType (string propertyName ){
Return typeof (T). GetProperty (propertyName). PropertyType;
}
Public static IList <T> GetDelegate_ToModelList (DataTable dt ){
IList <T> list = new List <T> ();
If (dt = null | dt. Rows. Count <1) return list;
Delegate setDelegate;
Foreach (DataRow dr in dt. Rows ){
T model = new T ();
Foreach (DataColumn dc in dt. Columns ){
SetDelegate = CreateSetDelegate (model, dc. ColumnName );
// Change the type here
SetDelegate. DynamicInvoke (Convert. ChangeType (dr [dc. ColumnName], GetPropertyType (dc. ColumnName )));
}
List. Add (model );
}
Return list;
}
}
}

Thank you. I just modified it. I can convert both SqlDataReader and DataTable. At that time, I only thought that I didn't know how to start every time I returned a specific type of delegation. I looked at your solution.
I didn't expect DynamicInvoke to be a learning method. Your code is clearly written. It is a pleasure to read it and learn from you!

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.