views:

40

answers:

1

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
        {
            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; }
        }
    }
+1  A: 

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.

Rewinder
@Rewinder yes this way it will work, but in my code it doesn't return all properties of IFoo, because IFoo inherits IEntity so it should have 2 properties not 1
Omu
@Omu According to MSDN, If you use the parameterless GetChildProperties, it returns the default PropertyDescriptorCollection (http://msdn.microsoft.com/en-us/library/d1ww6z63.aspx). I think this means it only returns the properties that are defined in the class, not the ones it inherits.
Rewinder
@Rewinder the Id property is not inherited by the class it is inherited by the implemented interface from another interface, I don't understand why would that matter, it looks like a bug to me
Omu