c 設定物件屬性 反射實體設定變數

2021-07-14 19:26:31 字數 2927 閱讀 6337

前台如果只傳遞了一兩個資料,在後台請求獲取資料設定屬性值還能夠接受,但是如果傳遞了大量的資料,就得多次httprequest reques[「***」]請求獲取資料設定屬性值,輸入的**量就很大,而且相當耗費時間,這種費時費力的方法是難以接受的。下面為大家提供乙個取巧的方法,獲取請求資料就變的很簡單了,而且可以節省時間。話不多說,直接上**:

c#**如下:

/// /// 設定物件屬性——反射實體設定變數

///

/// 物件

protected void set_property(object obj,del_property set) }}

}if (set != null) set(obj);

}

示例:

using system;

using system.data;

using system.collections.generic;

using dataobject.attrib;

using newtonsoft.json;

//部分實體

namespace tiaoceng.entity

/// ///訂單建構函式

///

/// 資料庫連線字串

public ordersmodel(string constring) : base(constring)

/// /// 訂單id

///

[dbcolumn(name = "or_id", size = 4, dbtype = sqldbtype.int, primarykey = true)]

[jsonproperty(propertyname = "or_id")]

public int? or_id

/// /// 購買者id,為0表示為非使用者id

///

[dbcolumn(name = "m_id", size = 4, dbtype = sqldbtype.int)]

[jsonproperty(propertyname = "member")]

public membermodel member

/// /// 購買者聯絡**

///

[dbcolumn(name = "or_buyertel", size = 50, dbtype = sqldbtype.varchar)]

[jsonproperty(propertyname = "or_buyertel")]

public string or_buyertel

/// /// 購買者位址

///

[dbcolumn(name = "or_buyeraddress", size = 200, dbtype = sqldbtype.nvarchar)]

[jsonproperty(propertyname = "or_buyeraddress")]

public string or_buyeraddress /// /// 已付款金額

///

[dbcolumn(name = "or_payprice", size = 9, dbtype = sqldbtype.decimal)]

[jsonproperty(propertyname = "or_payprice")]

public decimal? or_payprice /// /// 訂單狀態(0購物車,1待核實, 2待付款,3付款成功,4已發貨,5確認收貨,6申請退款,7退款成功,8退款失敗)

///

[dbcolumn(name = "or_progressstate", size = 4, dbtype = sqldbtype.int)]

[jsonproperty(propertyname = "or_progressstate")]

public int? or_progressstate

/// /// 訂單進度說明

///

[dbcolumn(name = "or_progress", size = 1000, dbtype = sqldbtype.nvarchar)]

[jsonproperty(propertyname = "or_progress")]

public string or_progress /// /// 狀態(0待審核,1審核通過,2審核失敗,3**站,4.作廢)

///

[dbcolumn(name = "or_state", size = 1, dbtype = sqldbtype.tinyint)]

[jsonproperty(propertyname = "or_state")]

public byte? or_state

....

}

//實體物件

ordersmodel _order = new ordersmodel();

//設定訂單資訊

set_property(_order, (obj) => ;

if (!string.isnullorempty(request["paydate"])) _order.or_paydate = datetime.parse(request["paydate"] + " 00:00:00");

if (!string.isnullorempty(request["shipdate"])) _order.or_shipdate = datetime.parse(request["shipdate"] + " 00:00:00");

_order.or_price = _price;

_order.or_amount = _num;

_order.or_getintegral = _getintegral;

});

C 使用反射設定屬性值

最近在refix乙個支援excel檔案匯入匯出功能時,發現有用到反射的相關技能。故而在網上查了些資料,通過 除錯加深下理解。class program var studentname getmodelvalue name student var studentcity setmodelvalue c...

C 按屬性名反射獲取和設定物件屬性值

using system using system.collections.generic using system.reflection namespace test public class product set private int pkid public string productna...

C 反射設定屬性值和獲取屬性值

獲取類中的屬性值 public string getmodelvalue string fieldname,object obj catch 設定類中的屬性值 public bool setmodelvalue string fieldname,string value,object obj cat...