此测试中的第3个断言失败但如果我将Id属性从IEntity移动到IFoo它将起作用
我需要获取所有属性,如何做到这一点? (whitout传递一个实例,由于某种原因,这种方式有效)
[TestFixture]
public class DescriptorTests
{
[Test]
public void Test()
{
var bar = new Bar {Name = "bar",Foo = new Foo {Id = 1, Name = "foo"}};
Assert.AreEqual(2, TypeDescriptor.GetProperties(bar).Count);
Assert.AreEqual(2, TypeDescriptor.GetProperties(bar.Foo).Count);
Assert.AreEqual(2, TypeDescriptor.GetProperties(bar)// this fails
.Find("Foo", false)
.GetChildProperties()
.Count); // the count is 1 instead of 2
}
public class Bar
{
public IFoo Foo { get; set; }
public string Name { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
public interface IFoo : IEntity
{
string Name { get; set; }
}
public class Foo : IFoo
{
public int Id { get; set; }
public string Name { get; set; }
}
}
答案 0 :(得分:1)
我不知道这是否是您正在寻找的,但如果您将特定实例传递给GetChildProperties(对象实例),它确实有效:
Assert.AreEqual(2, TypeDescriptor.GetProperties(bar)
.Find("Foo", false)
.GetChildProperties(bar.Foo)
.Count);
在您的代码中,它只返回类(IFoo)的属性,而不是实例。