如何检查继承的属性是否是class的一部分(sub-class)
How to check if inherited property is the part of class (sub-class)
我正在尝试创建一个通用函数,它可以通过查看 MemberExpression
的 Member
并在 Type
中找到它来判断 属性 存在于 class。它适用于普通属性,但对于继承属性,它找不到它们。
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();
}
输出:
Testing StudentID property
Property is part of Class
Testing FirstName property
Property is not part of Class
更新:
在@NetMage 的帮助下,我能够修改我的方法。请注意,我的方法现在还涵盖了表达式可以是不同的 sub-class 而 T
参数可以表示不同的 sub-class.
的情况
var employeeObj = new Employee(); // here Employee is also inherited from Person class
trickyTest = IsPropertyPartOfClass<Student, string>(x => employeeObj.FirstName);
在上面的例子中,我们想要函数 return 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();
}
lambda 中的 MemberInfo
和类型中的 PropertyInfo
之间的 ReflectedType
不同 - 也许您应该使用 HasSameMetadataDefinitionAs
?
查看讨论 here 了解添加它的原因。
可能 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));
}
我正在尝试创建一个通用函数,它可以通过查看 MemberExpression
的 Member
并在 Type
中找到它来判断 属性 存在于 class。它适用于普通属性,但对于继承属性,它找不到它们。
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();
}
输出:
Testing StudentID property
Property is part of Class
Testing FirstName property
Property is not part of Class
更新:
在@NetMage 的帮助下,我能够修改我的方法。请注意,我的方法现在还涵盖了表达式可以是不同的 sub-class 而 T
参数可以表示不同的 sub-class.
var employeeObj = new Employee(); // here Employee is also inherited from Person class
trickyTest = IsPropertyPartOfClass<Student, string>(x => employeeObj.FirstName);
在上面的例子中,我们想要函数 return 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();
}
lambda 中的 MemberInfo
和类型中的 PropertyInfo
之间的 ReflectedType
不同 - 也许您应该使用 HasSameMetadataDefinitionAs
?
查看讨论 here 了解添加它的原因。
可能 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));
}