[英]How do I use reflection to get the nested property
我正在為ASP.NET編寫TwoWay綁定控件。 我終於把事情做好了。 我使用以下代碼重新綁定:
private void RebindData()
{
// return if the DataValue wasn't loaded from ViewState
if (DataValue == null)
return;
// extract the values from the two-way binding template
IOrderedDictionary values = new OrderedDictionary();
IBindableTemplate itemTemplate = DataTemplate as IBindableTemplate;
if (itemTemplate != null)
{
foreach (DictionaryEntry entry in itemTemplate.ExtractValues(this))
{
values[entry.Key] = entry.Value;
}
}
// use reflection to push the bound fields back to the datavalue
foreach(var key in values.Keys)
{
var type = typeof(T);
var pi = type.GetProperty(key.ToString());
if (pi != null)
{
pi.SetValue(DataValue, values[key], null);
}
}
}
這適用於像這樣的簡單情況:
<%# Bind("Name") %>
當我有這樣的嵌套綁定語句時,我正在使用的反射技術不起作用:
<%# Bind("Customer.Name") %>
有沒有簡單的方法可以將反射用於這樣的嵌套屬性? 我應該為此使用遞歸函數還是僅使用循環?
為什么不使用已經支持此功能的ASP.NET DataBinder?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.