通過特性動態獲取屬性及值

2021-09-08 23:37:48 字數 2001 閱讀 9702

using

system.collections.generic;

using

system.data.sqlclient;

using

system.data;

using

system;

namespace

npetshop.domain

//////

獲取或設定name欄位的值  system.string

///資料庫型別長度:varchar 大小:50 

///

[column(

"name")]

public

system.string name

//////

獲取或設定userid欄位的值  system.int32

///資料庫型別長度:int 大小:4 精度:10

///

[column(

"userid")]

public

system.int32 userid

#endregion

#region

建構函式

//////

建構函式

///

///name

///userid

public

productentity(system.string name,system.int32 userid)

//////

建構函式

///

public

productentity()

#endregion}}

//特性

using

system;

using

system.collections.generic;

using

system.text;

using

system.data;

namespace

npetshop.domain

//////

獲取或設定列的標題。

///

public

string

columncaption 

//////

獲取或設定列的型別。

///

public

sqldbtype columntype 

//////

初始化 magicstar.entity.columnattribute 類的乙個新例項。

///

///表或檢視的標題。

///表或檢視的名稱。

///型別。

public

columnattribute(

string

columnname, 

string

columncaption, sqldbtype columntype)

//////

初始化 magicstar.entity.columnattribute 類的乙個新例項。

///

///表或檢視的標題。

///表或檢視的名稱。

///型別。

public

columnattribute(

string

columnname)}}

///實現

productentity _roductentity 

=new

productentity();

_roductentity.id =1

;_roductentity.name ="

2";type type 

=_roductentity.gettype();

propertyinfo properties 

=type.getproperties();

foreach

(propertyinfo property 

inproperties)

通過特性動態獲取屬性及值

using system.collections.generic using system.data.sqlclient using system.data using system namespace npetshop.domain 獲取或設定name欄位的值 system.string 資料庫型...

通過反射獲取實體物件的屬性及對應的屬性值

需求 需要獲取實體物件的屬性及屬性值作為key value形式的namevaluepair引數,用於post請求的引數,為了避免一大段的get 考慮使用反射方式進行設定,示例 如下 public class person public void setname string name public ...

C 通過特性驗證實體屬性值

一,什麼是特性 特性也是一種物件,特殊之處在於其編譯時就存在了,也就是在程式執行之前就存在了。二,如何定義乙個特性 attributeusage attributetargets.property attributetargets.field public sealed class required...