Я пытался выполнить общие методы и использовать рекурсию. Проблема заключается в том, что метод 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