2 回答

TA貢獻1845條經驗 獲得超8個贊
好的,我找到了一種方法來實現 Pac0 所說的反射,你可以做到這一點
public void FindItem<T1>(T1 id)
{
try
{
var obj = this;
// fill the object with the DB data
obj = Dal.ObjFind(new Production.ProductCategory(), id);
PropertyDescriptorCollection PropertyObj =
TypeDescriptor.GetProperties(this);
//iterating the properties in the instance of the class
foreach (PropertyDescriptor prop in PropertyObj)
{
//Get the value for each properties in the filled Obj
//and set that value for each properites in "this"
prop.SetValue (this,prop.GetValue(obj));
}
}
catch (Exception e)
{
throw e;
}
}
通過這種方式,您可以像“test.FindItem(1)”一樣調用您的安裝程序,并且對象將被加載,謝謝!

TA貢獻1816條經驗 獲得超4個贊
要在運行時獲取有關某個類 durig 運行時的信息,稱為 *reflection**(該術語應該可以幫助您進行搜索)
要獲取類型的所有屬性,您可以使用Type.GetProperties()。
也看看GetProperty。
有了這個,你應該能夠實現你想要的。
但是,我不相信這是最簡單的方法??赡苡腥藭氤龈玫姆椒▉斫鉀Q您的潛在問題。
- 2 回答
- 0 關注
- 256 瀏覽
添加回答
舉報