나는 일반적인 방법을 실행하고 재귀를 사용하려고 노력 해왔다. 문제는 메서드 GetMethod
이 null
을 반환한다는 것입니다. 코드를 개선하려면 어떻게해야합니까? 그런 방법이 없기 때문에제네릭 메서드를 호출하려고하면 null이 반환되는 이유는 무엇입니까?
public static T GetElementObject<T>(this XElement element)
{
T returnObject = Activator.CreateInstance<T>();
PropertyInfo[] propertyInfos = returnObject.GetType().GetProperties();
Type propertyType;
foreach (PropertyInfo propertyInfo in propertyInfos)
{
propertyType = propertyInfo.PropertyType;
if (propertyType.IsAssignableFrom(typeof(BaseProxyEntity)))
{
MethodInfo getElementObject = typeof(Utility).GetMethod("GetElementObject<>", System.Reflection.BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(propertyType);
propertyInfo.SetValue(returnObject, getElementObject.Invoke(null, new object[] { element.Descendants(propertyInfo.Name) }), null);
}
else if (propertyType.IsValueType == true)
{
MethodInfo CastValue = typeof(Utility).GetMethod("CastValue<>", System.Reflection.BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(propertyType);
propertyInfo.SetValue(returnObject, CastValue.Invoke(null, new object[] { element.Attribute(propertyInfo.Name).Value }), null);
}
//Other else conditions ...
}
return returnObject;
}
아마도 도움이되는 링크 : http://msdn.microsoft.com/en-us/library/ms172334.aspx – Mikeb
바인딩 플래그가 맞습니까? Utility 클래스의 코드를 표시 할 수 있습니까? –
Utility 클래스에는 특별한 것이 없습니다. 위에 나온 것과 같은 정적 메서드가 있습니다. 재귀 적으로 호출되는 "GetElementObject"에 대한 메서드 정의를 볼 수 있습니다. – wasimbhalli