[英]Finding properties with simple getters/setters of a DTO class
在我的DTO類中,我具有一些具有簡單getter / setter( get; set;
)的屬性,而某些屬性具有更復雜的getter / setter(例如,通過其他一些屬性/變量評估值等)
[Serializable]
public class MyClassDto
{
public virtual string Name { get; set; }
public virtual string Description { get { return Name; } set { Name = value; } }
....
}
在上面的示例中,我想要獲取屬性Name
,但不想獲取Description
我正在嘗試使用PropertyInfo
查找具有get;set;
屬性get;set;
作為getter / setter,但沒有這樣做。
我之所以如此發展是因為: 簡單的get;set;
屬性 在我的Entity類中具有一個具有相同名稱的屬性,並且在數據庫表中具有一列。 我的實體還具有一些屬性,這些屬性在數據庫表上沒有相關列。 因此,使用簡單的getter / setter獲取屬性將為我提供具有相關數據庫列的屬性。
同時,我正在使用NHibernate。
檢查這是否滿足您的要求。
var property = typeof(MyClassDto).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(t => t.CanRead && t.CanWrite);
foreach (var item in property)
{
string propertyName = item.Name;
bool CompilerGenerated = item.GetGetMethod()
.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Any();
//Description is not CompilerGeneratedAttribute so return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.