This is a two-part question. First, which of these explicit property implementations gets bound to IAllTogether.SomeInt
and why doesn't the compiler complain about ambiguity? It does when you comment-out the marked lines.
public interface IFirst
{
int SomeInt { get; }
}
public interface ISecond
{
int SomeInt { get; }
}
public interface ICombined : IFirst, ISecond
{
new int SomeInt { get; } // Comment this line.
}
public interface IAllTogether : IFirst, ISecond, ICombined
{ }
public sealed class Implementation : IAllTogether
{
int ICombined.SomeInt { get { return 0; } } // Comment this line.
int IFirst.SomeInt { get { return 0; } }
int ISecond.SomeInt { get { return 0; } }
}
IAllTogether t = new Implementation();
var unimportant = t.SomeInt;
Second question would be: how do I find the right PropertyInfo
when given an interface Type
and a name of the property? I can use GetInterfaces()
and GetProperty()
to list all the possible candidates, but how do I know which is the right one? I tried typeof(IAllTogether).GetProperty("SomeInt")
, but it doesn't work.
Edit
Looks like the answer to the first part is that the hiding of inherited members resolves ambiguity. However, not even a single comment yet on the second part: how to reliably find the proper PropertyInfo
for some property name and an interface type.
Edit 2
Clarification on the second part of the question. What I'm looking for is a way to get the right property for any unknown Type
. Basically, a method like this:
public static PropertyInfo GetPropertyOfInterface(Type interfaceType, string propertyName)
{
if (!interfaceType.IsInterface)
throw new ArgumentException();
// for interfaceType == typeof(IAllTogether), return ICombined.SomeInt
// for interfaceType == typeof(IFirst), return IFirst.SomeInt
}