最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#中利用委托反射將DataTable轉(zhuǎn)換為實體集的代碼

 更新時間:2012年10月11日 01:05:14   作者:  
c#中利用委托反射將DataTable轉(zhuǎn)換為實體集的代碼,需要的朋友可以參考下
類泛型的約束:
復(fù)制代碼 代碼如下:
public static class ToModel<T> where T : class, new()

定義委托:
復(fù)制代碼 代碼如下:
public delegate void SetString(string value);

創(chuàng)建委托方法:
復(fù)制代碼 代碼如下:

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;
}

利用反射和委托將DataTable轉(zhuǎn)換為實體集:
復(fù)制代碼 代碼如下:

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;
}

這樣寫問題就來了,因為委托定義的參數(shù)時string類型的,因為我們實體中可能有int或者DateTime類型的,這時就需要用上泛型委托了
如果這樣定義委托:
復(fù)制代碼 代碼如下:
public delegate void SetString<PT>(PT value)

創(chuàng)建委托方法(這里有問題,不知如何處理):
復(fù)制代碼 代碼如下:

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>;
}

利用反射和委托將DataTable轉(zhuǎn)換為實體集:
復(fù)制代碼 代碼如下:

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;
}

一直疑惑著,希望有人幫我解決疑惑,直接反射的方法我也有,但是這個問題不解決,心里一直有疙瘩,希望有人幫幫忙,謝謝
泛型可以動態(tài)構(gòu)建的,你了解了這個,就能解決了,附上我的簡略代碼:
復(fù)制代碼 代碼如下:

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();
//這里構(gòu)造泛型委托類型
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);
//這里改變類型
setDelegate.DynamicInvoke(Convert.ChangeType(dr[dc.ColumnName], GetPropertyType(dc.ColumnName)));
}
list.Add(model);
}
return list;
}
}
}

謝謝,我剛修改了,我傳進(jìn)去SqlDataReader和DataTable都可以轉(zhuǎn)換了,當(dāng)時只想著每次返回一個特定類型等委托都不知道如何下手,看著你的方法解決了
沒想到DynamicInvoke這個方法,算是學(xué)習(xí)了,你的代碼寫著層次好清晰,看了是一種享受,向你學(xué)習(xí)??!

相關(guān)文章

  • C#實現(xiàn)簡單打字小游戲

    C#實現(xiàn)簡單打字小游戲

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡單打字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C#常用數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array

    C#常用數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array

    這篇文章介紹了C#常用數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組Array,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C# 9 中新加入的關(guān)鍵詞 init,record,with

    C# 9 中新加入的關(guān)鍵詞 init,record,with

    這篇文章主要介紹了C# 9 中新加入的關(guān)鍵詞 init,record,with的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c# 9,感興趣的朋友可以了解下
    2020-08-08
  • C#中Linq延遲查詢的例子

    C#中Linq延遲查詢的例子

    這篇文章主要介紹了C#中Linq延遲查詢的例子,本文用一個實例來講解延遲查詢的使用,需要的朋友可以參考下
    2015-06-06
  • C#類的成員之Field字段的使用

    C#類的成員之Field字段的使用

    本文主要介紹了C#類的成員之Field字段的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法

    C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法

    這篇文章主要介紹了C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法,涉及C#URL及字符串操作的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C# 定時器定時更新的簡單實例

    C# 定時器定時更新的簡單實例

    這篇文章主要介紹了C#中定時器定時更新的簡單實例。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12
  • C#使用CefSharp和網(wǎng)頁進(jìn)行自動化交互的示例代碼

    C#使用CefSharp和網(wǎng)頁進(jìn)行自動化交互的示例代碼

    CefSharp 是一個用 C# 編寫的開源庫,它封裝了 Google Chrome 瀏覽器的 Chromium 內(nèi)核,CefSharp 允許開發(fā)者在其應(yīng)用程序中嵌入瀏覽器功能,從而能夠展示網(wǎng)頁內(nèi)容、執(zhí)行JavaScript代碼,本文給大家介紹了C#使用CefSharp和網(wǎng)頁進(jìn)行自動化交互,需要的朋友可以參考下
    2024-07-07
  • 設(shè)置C#窗體程序只能啟動一次

    設(shè)置C#窗體程序只能啟動一次

    有時候我們需要我們的軟件只可以啟動一次,用下面的代碼就可以實現(xiàn)啊。
    2009-04-04
  • C#文件路徑操作詳細(xì)總結(jié)

    C#文件路徑操作詳細(xì)總結(jié)

    本篇文章主要是對C#中的文件路徑操作進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01

最新評論

沙田区| 滨州市| 芷江| 儋州市| 漯河市| 汪清县| 济阳县| 吉安县| 巍山| 哈密市| 宜黄县| 杭锦后旗| 庄河市| 云霄县| 桦甸市| 绥芬河市| 洛扎县| 梁河县| 湄潭县| 永宁县| 香港 | 获嘉县| 东乡| 东莞市| 石阡县| 胶州市| 黄山市| 江陵县| 西平县| 宁津县| 婺源县| 兴文县| 溧阳市| 岳阳县| 巴中市| 新巴尔虎右旗| 吉首市| 桦南县| 凤城市| 永仁县| 延庆县|