C# 从类中通过反射获取属性
假设我们有以下TestClassC# 从类中通过反射获取属性,c#,C#,假设我们有以下TestClass public interface IBase { int MyIntegerAA { get; set; } } public interface F : G { string MyStringBB { get; set; } } public interface E : F { string MyStringCC { get; set; } IBase xyz { get; set; } } public interfac
public interface IBase
{
int MyIntegerAA { get; set; }
}
public interface F : G
{
string MyStringBB { get; set; }
}
public interface E : F
{
string MyStringCC { get; set; }
IBase xyz { get; set; }
}
public interface D : E
{
int myint1 { get; }
int myint2 { get; }
int myint3 { get; }
int myint4 { get; }
}
public interface C : D
{
string MyString1{ get; set; }
}
public class B : C
{
public int myint1 { get; }
public int myint2 { get; }
public int myint3 { get; }
public int myint4 { get; }
public string MyString1 { get; set; }
public IBase xyz { get; set; }
}
public class A : B
{
public string test1 { get; set; }
}
问题是,我们如何“枚举”每个类和接口,以仅返回其声明的属性,而不在枚举期间引用接口IBase
比如说
如果我试图得到类B的属性,那么我会得到以下结果
当前父级是:GetProperties.Form1+TestClass+B
公共财产数量:6
属性名称:xyz
属性类型:[GetProperties.Form1+TestClass+IBase]
属性名称:myint1
属性类型:[System.Int32]
属性名称:myint2
属性类型:[System.Int32]
属性名称:myint3
属性类型:[System.Int32]
属性名称:myint4
属性类型:[System.Int32]
属性名称:MyString1
属性类型:[System.String]
有没有办法不包括
属性名称:xyz
属性类型:[GetProperties.Form1+TestClass+IBase]
?它是在
B
类中声明的,所以在遍历所有属性时,除了忽略它之外,没有其他方法。通过指定System.Reflection.BindingFlags.DeclaredOnly
,您可以从搜索中删除派生属性。它在B
类中声明,所以在遍历所有属性时,除了忽略它之外,别无他法。通过指定System.Reflection.BindingFlags.DeclaredOnly
,可以从搜索中删除派生属性。