The Blue Rage

Consider the following:

Code Snippet

namespace foo{

public interface IFoo{

int Foo{

get;

}

}

public interface IBar: IFoo{

int Bar{

get;

}

}

}

I would expect a call to:

Code Snippet
TypeDescriptor.GetProperties (
typeof ( IBar ) );

to return a collection of two properties {Foo and Bar}, yet it only returns the Bar property from the IBar interface. This seems like a bug in the TypeDescriptor implementation because the C# language specification indicates that interfaces inherit the properties, methods, events, etc... from their base interfaces. Furthermore, a similar excercise with classes works as expected. Does anyone have any insight regarding this



Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

CommonGenius.com

I would tend to agree with you, and I can't find anything in the documentation that would indicate otherwise. Unless someone else has a reason why this would be the case, I would suggest filing a bug report at https://connect.microsoft.com/VisualStudio/Feedback. At the very least the documentation should be updated.




Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

nobugz

System.Reflection and the metadata in an assembly has no notion of inherited interfaces. You'll see this by running this sample code:

Type baset = typeof(IFoo).BaseType;
bool isbase = typeof(IFoo).IsSubclassOf(typeof(IBar));

"baset" is null and "isbase" is false. It is the compiler that implements interface inheritance.





Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

CommonGenius.com

Of course the metadata knows about interface inheritance. For example, run the following code:

Code Snippet

bool doesInherit= typeof(IFoo).IsAssignableFrom(typeof(IBar));

doesInherit is true. (And btw, your IsSubclassOf test is backwards. You are testing if IFoo is a subclass of IBar, but you want to test the other way around. It still comes out false though, because IsSubclassOf only deals with classes. IsAssignableFrom also deals with interfaces.)

However, I did find an explanation for the behavior of GetProperties. From the ECMA CLI specification:

8.10 Member inheritance

Only object types can inherit implementations, hence only object types can inherit members (see ¡́8.9.8). While

interface types can be derived from other interface types, they only ¡°inherit¡± the requirement to implement

method contracts, never fields or method implementations.

So IBar inherits the requirement to implement the Foo property, but not the Foo property itself.

Still seems a bit shady to me, but its all there in black and white.






Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

The Blue Rage

I think we all would agree that the following code is valid.

IBar bar = object as IBar;

int i = bar.Foo;

I do not have to cast to an IFoo to have the property available and Intellisense is smart enough to show the property on the interface. Why am I wrong to expect the property to show up when using the TypeDescriptor





Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

CommonGenius.com

It just comes down to the definition of inheritance when referring to interfaces. Interfaces do not inherit members from their base interfaces, they inherit requirement. The compiler can use that "requirement inheritance" to bind to the Foo property from and IBar reference, but the property itself has not actually been inherited. It might help to read through some of the CLI specification that I posted. Not the best answer I know, but its the best I've got.

You might want to file a suggestion at the Product Feedback Center, recommending that there be a way to get the "inherited requirements" from a derived interface.






Re: .NET Base Class Library TypeDescriptor.GetProperties () : Interface Issue

GrantaGuy

This issue is causing me a real headache when it comes to databinding. I've been using an 'object' as a data source - except in this instance it is in fact an interface. This is done because we have two distinct implementations of these interfaces, for differing data access.

The problem arises because I have an interface like this:

public interface INamedObject

{

string Name { get; }

}

this gets inherited by other interfaces:

public interface IAttribute : INamedObject

{

int Type { get; }

}

When I add IAttribute as a datasource, the name property does not show up; and I cannot bind to it!

Granta Guy