리플렉션을 사용하여 아래와 같은 개체 속성을 설정할 수 있음을 알고 있습니다.Reflections - 속성이 List에있을 때 개체 속성 설정
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 string BigHeader { get; set; }
를 들어,
public class CorePage
{
public string BigHeader { get; set; }
public List<BigLinks> BigLinks { get; set; }
}
public class BigLinks
{
public string TextContent { get; set; }
}
속성이 설정하는 경우입니다 분명 작동 -method 내 SaveContent()
그러나 속성 나는 경우 어떻게 이렇게 할 수 있습니다 방법 세트 수, 5 BigLinks
객체의 목록입니다
public List<BigLinks> BigLinks { get; set; }
public List<BigLinks> BigLinks { get; set; }
경우 : 설정하고자하는 재산에 예를 들어 세 번째 객체의 값 public string TextContent { get; set; }
?
var c = new CorePage() { BigLinks = new List<BigLinks> { new BigLinks { TextContent = "Y"}}};
var r = typeof(CorePage).GetProperty("BigLinks").GetGetMethod().Invoke(c, null) as List<BigLinks>;
r[0].TextContent = "X";
당신이 목록 항목의 유형을 모르는 경우가 :
var itemInList = (typeof(CorePage).GetProperty("BigLinks").GetGetMethod().Invoke(c, null) as IList)[0];
itemInList.GetType().GetProperty("TextContent").SetValue(itemInList, "XXX", null);
또 다른 옵션은
콘텐츠를 직렬화하려고합니까? – Hammerstein
이 링크는 도움이 될 수 있습니다 : http://stackoverflow.com/questions/291359/c-sharp-reflection-indexed-properties – DLeh