【发布时间】:2014-11-02 02:57:01
【问题描述】:
我知道我可以使用反射来设置对象属性,如下所示。
public void SaveContent(string propertyName, string contentToUpdate, string corePageId)
{
var page = Session.Load<CorePage>(corePageId);
Type type = page.GetType();
PropertyInfo prop = type.GetProperty(propertyName);
prop.SetValue(page, contentToUpdate, null);
}
我在下面有这些课程:
public class CorePage
{
public string BigHeader { get; set; }
public List<BigLinks> BigLinks { get; set; }
}
public class BigLinks
{
public string TextContent { get; set; }
}
当要设置的属性是 public string BigHeader { get; set; } 时,我的 SaveContent() 方法显然有效
但是如果我要设置的属性在属性中,我该怎么做:
public List<BigLinks> BigLinks { get; set; }
如果public List<BigLinks> BigLinks { get; set; } 是5 个BigLinks 对象的列表,如何设置例如第三个对象public string TextContent { get; set; } 的值?
【问题讨论】:
-
您是否要序列化内容?
-
此链接可能有帮助:stackoverflow.com/questions/291359/…
标签: c# system.reflection