Constraints for class generics:
Definition Delegation:
Create a delegate method:
private static SetString CreateStringDelegate(T model, string propertyName)
{
MethodInfo mi = ().GetProperty(propertyName).GetSetMethod();
Type type = typeof(SetString);
return (type, model, mi) as SetString;
}
Convert DataTable to entity set using reflection and delegate:
public static IList<T> GetDelegate_ToModelList(DataTable dt)
{
IList<T> list = new List<T>();
if (dt == null || < 1) return list;
SetString setDelegateString;
foreach (DataRow dr in )
{
T model = new T();
foreach (DataColumn dc in )
{
setDelegateString = CreateStringDelegate(model, );
setDelegateString(dr[].ToString());
}
(model);
}
return list;
}
This is the problem of writing, because the delegate defines the parameters string type, because there may be int or DateTime type in our entity, so we need to use the generic delegate
If the delegate is defined like this:
Create a delegate method (there is a problem here, I don't know how to deal with it):
private static SetString CreateStringDelegate(T model, string propertyName)
{
MethodInfo mi = ().GetProperty(propertyName).GetSetMethod();
Type type = typeof(model).GetProperty(propertyName).PropertyType;
return (type, model, mi) as SetString<type>;
}
Convert DataTable to entity set using reflection and delegate:
public static IList<T> GetDelegate_ToModelList(DataTable dt)
{
IList<T> list = new List<T>();
if (dt == null || < 1) return list;
foreach (DataRow dr in )
{
T model = new T();
foreach (DataColumn dc in )
{
SetString<typeof(T).GetProperty().PropertyType> setDelegateString = CreateStringDelegate(model, );
setDelegateString(dr[].ToString());
}
(model);
}
return list;
}
I have been puzzled and hope that someone will help me solve my doubts. I have a direct reflection method, but if this problem is not solved, I have always felt a knot in my heart. I hope someone will help me. Thank you
Generics can be constructed dynamically. Once you understand this, you can solve it. Attach my brief code:
using System;
using ;
using ;
using ;
using ;
using ;
namespace RftToModel {
class Program {
static void Main(string[] args) {
var result = ToModel<TestModel>.GetDelegate_ToModelList(BuildSampleTable());
foreach (var item in result) {
(item);
}
();
}
static DataTable BuildSampleTable() {
DataTable result = new DataTable();
("ID", typeof(int));
("Name", typeof(string));
("IsDeleted", typeof(bool));
(new object[] { 1, "", false });
(new object[] { 2, "", 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 ("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 = ().GetProperty(propertyName).GetSetMethod();
//Construct the generic delegate type here
Type delType = typeof(SetValue<>).MakeGenericType(GetPropertyType(propertyName));
return (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 || < 1) return list;
Delegate setDelegate;
foreach (DataRow dr in ) {
T model = new T();
foreach (DataColumn dc in ) {
setDelegate = CreateSetDelegate(model, );
//Change the type here
((dr[], GetPropertyType()));
}
(model);
}
return list;
}
}
}
Thank you, I just modified it. I passed it in and both SqlDataReader and DataTable can be converted. At that time, I just thought about returning a specific type and other delegation every time I didn't know how to start. I watched your method solve it.
I didn’t expect that DynamicInvoke’s method was considered to be a learning process. Your code is written with clear levels. It’s a pleasure to read it. Learn from you!
Copy the codeThe code is as follows:
public static class ToModel<T> where T : class, new()
Definition Delegation:
Copy the codeThe code is as follows:
public delegate void SetString(string value);
Create a delegate method:
Copy the codeThe code is as follows:
private static SetString CreateStringDelegate(T model, string propertyName)
{
MethodInfo mi = ().GetProperty(propertyName).GetSetMethod();
Type type = typeof(SetString);
return (type, model, mi) as SetString;
}
Convert DataTable to entity set using reflection and delegate:
Copy the codeThe code is as follows:
public static IList<T> GetDelegate_ToModelList(DataTable dt)
{
IList<T> list = new List<T>();
if (dt == null || < 1) return list;
SetString setDelegateString;
foreach (DataRow dr in )
{
T model = new T();
foreach (DataColumn dc in )
{
setDelegateString = CreateStringDelegate(model, );
setDelegateString(dr[].ToString());
}
(model);
}
return list;
}
This is the problem of writing, because the delegate defines the parameters string type, because there may be int or DateTime type in our entity, so we need to use the generic delegate
If the delegate is defined like this:
Copy the codeThe code is as follows:
public delegate void SetString<PT>(PT value)
Create a delegate method (there is a problem here, I don't know how to deal with it):
Copy the codeThe code is as follows:
private static SetString CreateStringDelegate(T model, string propertyName)
{
MethodInfo mi = ().GetProperty(propertyName).GetSetMethod();
Type type = typeof(model).GetProperty(propertyName).PropertyType;
return (type, model, mi) as SetString<type>;
}
Convert DataTable to entity set using reflection and delegate:
Copy the codeThe code is as follows:
public static IList<T> GetDelegate_ToModelList(DataTable dt)
{
IList<T> list = new List<T>();
if (dt == null || < 1) return list;
foreach (DataRow dr in )
{
T model = new T();
foreach (DataColumn dc in )
{
SetString<typeof(T).GetProperty().PropertyType> setDelegateString = CreateStringDelegate(model, );
setDelegateString(dr[].ToString());
}
(model);
}
return list;
}
I have been puzzled and hope that someone will help me solve my doubts. I have a direct reflection method, but if this problem is not solved, I have always felt a knot in my heart. I hope someone will help me. Thank you
Generics can be constructed dynamically. Once you understand this, you can solve it. Attach my brief code:
Copy the codeThe code is as follows:
using System;
using ;
using ;
using ;
using ;
using ;
namespace RftToModel {
class Program {
static void Main(string[] args) {
var result = ToModel<TestModel>.GetDelegate_ToModelList(BuildSampleTable());
foreach (var item in result) {
(item);
}
();
}
static DataTable BuildSampleTable() {
DataTable result = new DataTable();
("ID", typeof(int));
("Name", typeof(string));
("IsDeleted", typeof(bool));
(new object[] { 1, "", false });
(new object[] { 2, "", 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 ("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 = ().GetProperty(propertyName).GetSetMethod();
//Construct the generic delegate type here
Type delType = typeof(SetValue<>).MakeGenericType(GetPropertyType(propertyName));
return (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 || < 1) return list;
Delegate setDelegate;
foreach (DataRow dr in ) {
T model = new T();
foreach (DataColumn dc in ) {
setDelegate = CreateSetDelegate(model, );
//Change the type here
((dr[], GetPropertyType()));
}
(model);
}
return list;
}
}
}
Thank you, I just modified it. I passed it in and both SqlDataReader and DataTable can be converted. At that time, I just thought about returning a specific type and other delegation every time I didn't know how to start. I watched your method solve it.
I didn’t expect that DynamicInvoke’s method was considered to be a learning process. Your code is written with clear levels. It’s a pleasure to read it. Learn from you!