[英]How to check if inherited property is the part of class (sub-class)
I am trying to create a generic function that can tell by looking in the MemberExpression
's Member
and finding it in a Type
to see if that property exists in that class.我正在尝试创建一个通用函数,该函数可以通过查看
MemberExpression
的Member
并在Type
找到它来判断该属性是否存在于该类中。 It works fine with normal properties but with inherited properties it does not find them.它适用于普通属性,但对于继承的属性,它找不到它们。
class Person {
public string FirstName {get;set;}
public string LastName {get;set;}
}
class Student : Person {
public string StudentID {get;set;}
}
public static void Main()
{
bool test1 = IsPropertyPartOfClass<Student, string>(x => x.StudentID);
Console.WriteLine("Testing StudentID property");
if (test1)
Console.WriteLine("\tProperty is part of Class");
else
Console.WriteLine("\tProperty is not part of Class");
bool test2 = IsPropertyPartOfClass<Student, string>(x => x.FirstName);
Console.WriteLine("Testing FirstName property");
if (test2)
Console.WriteLine("\tProperty is part of Class");
else
Console.WriteLine("\tProperty is not part of Class");
}
public static bool IsPropertyPartOfClass<T, R>(Expression<Func<T, R>> expPropSel){
MemberInfo mem_info_from_exp = ((MemberExpression)((LambdaExpression)expPropSel).Body).Member;
return typeof(T).GetProperties().Where(x=> x == mem_info_from_exp).Any();
}
Output:输出:
Testing StudentID property
Property is part of Class
Testing FirstName property
Property is not part of Class
Update:更新:
With the help of @NetMage I was able to modify my method.在@NetMage 的帮助下,我能够修改我的方法。 Notice that my method is now also covering the cases where expression could be of different sub-class while
T
parameter could be representing different sub-class.请注意,我的方法现在还涵盖了表达式可能属于不同子类而
T
参数可能代表不同子类的情况。
var employeeObj = new Employee(); // here Employee is also inherited from Person class
trickyTest = IsPropertyPartOfClass<Student, string>(x => employeeObj.FirstName);
In above example, we want function to return false
.在上面的例子中,我们希望函数返回
false
。
public static bool IsPropertyPartOfClass<T, R>(Expression<Func<T, R>> expPropSel) {
MemberInfo mem_info_from_exp = ((MemberExpression)((LambdaExpression)expPropSel).Body).Member;
Type sourceType = ((MemberExpression)((LambdaExpression)expPropSel).Body).Expression.Type;
return typeof(T)
.GetProperties()
.Where(x=>
sourceType == typeof(T) &&
(
(x == mem_info_from_exp) ||
(
x.Name == mem_info_from_exp.Name &&
x.Module.Equals(mem_info_from_exp.Module) &&
x.MetadataToken == mem_info_from_exp.MetadataToken
)
)
)
.Any();
}
The ReflectedType
is different between the MemberInfo
from the lambda and the PropertyInfo
s from the type - perhaps you should use HasSameMetadataDefinitionAs
?该
ReflectedType
是之间是不同MemberInfo
从拉姆达和PropertyInfo
■从类型-也许你应该使用HasSameMetadataDefinitionAs
?
See the discussion here for why it was added.请参阅此处的讨论以了解添加它的原因。
Probably IsPropertyPartOfClass method can check for base properties as well可能 IsPropertyPartOfClass 方法也可以检查基本属性
public static bool IsPropertyPartOfClass<T, R>(Expression<Func<T, R>>
expPropSel)
{
MemberInfo memInfoFromExp = ((MemberExpression) expPropSel.Body).Member;
var memberInfo = typeof(T).BaseType;
return typeof(T).GetProperties().Any(x => x == memInfoFromExp ||
memberInfo != null && memberInfo.GetProperties().Any(x => x ==
memInfoFromExp));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.