기본 속성 설정 : 내가 객체에 전달하고이 방법은 재귀 적으로 자식 개체와 개체를 인스턴스화 곳 C#을 재귀 반사 및 일반 목록 나는 다음과 같은 달성하기 위해 반사를 사용하려고
내가하는 방법이 필요하고 기본 값으로 등록 정보를 설정하십시오. 필요에 따라 여러 수준으로 인스턴스화 된 전체 개체가 필요합니다.이 메서드는 다른 개체의 일반 목록이 될 여러 속성을 가진 개체를 처리 할 수 있어야합니다.
private void SetPropertyValues(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && property.PropertyType.FullName.Contains("BusinessObjects"))
{
Type propType = property.PropertyType;
var subObject = Activator.CreateInstance(propType);
SetPropertyValues(subObject);
property.SetValue(obj, subObject, null);
}
else if (property.PropertyType == typeof(string))
{
property.SetValue(obj, property.Name, null);
}
else if (property.PropertyType == typeof(DateTime))
{
property.SetValue(obj, DateTime.Today, null);
}
else if (property.PropertyType == typeof(int))
{
property.SetValue(obj, 0, null);
}
else if (property.PropertyType == typeof(decimal))
{
property.SetValue(obj, 0, null);
}
}
}
감사
오류가 발생한 행을 확인하는 것이 도움이됩니다. 또한 형식 이름에 BusinessObjects가 포함 된 속성에 대해서만 새 개체를 만드는 것처럼 보입니다.이 속성이 사용자의 의도인지 확실하지 않습니다. –