PropertyDescriptor GetChildProperties doesn't return properties from type extending interface which inherited another interface
the 3rd assert in this test fails but if I would move the Id property from IEntity to IFoo it will work
I need to get all the properties, how to do this ? (whitout passing an instance, for some reason this way works)
[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
{
开发者_如何学Go 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; }
}
}
I don't know if this is what you are looking for, but if you pass the specific instance into GetChildProperties(object instance) it does work:
Assert.AreEqual(2, TypeDescriptor.GetProperties(bar)
.Find("Foo", false)
.GetChildProperties(bar.Foo)
.Count);
In your code it would only return the properties of the class (IFoo) and not the instance.
精彩评论