Member of type multiple interfaces
I am trying to declare a Property (or Function for that matter) of a type that satisfies several interfaces. I'm assuming this can't be done in C# or VB. My question is, if it makes sense to define a type that implements multiple interfaces, why cant you define a member of such? Ex. I can do this
interface IBar
{ String BarMember; }
interface IFoo
{ String FooMember; }
class FooBar : IBar, IFoo
{
public string BarMember{get;set;}
public string FooMember{get;set;}
}
so why cant i do this
class SomeClass
{
public {IBar, IFoo} FooBarMember {get;set;}
}
in this case FooBar would satisfy SomeClass.FooBarMember?
The reason I need this is simply that i need a member that satisfies those interface requirements. I dont care what the actual concre开发者_Python百科te class is. I know i can combine both of the interfaces by creating a new interface that combines both of them, but why should i have to do that?
OK, now that I finally understood the question: generics to the rescue!
class SomeClass<T> where T : IFoo, IBar
{
public T FooBarMember { get; set; }
}
Now, FooBarMember
will be of type T
, which is a type that must implement both IFoo
and IBar
.
Consider this:
class A : IBar
{
public string SomeMember { get; set; }
}
class B : IFoo
{
public string SomeMember { get; set; }
}
class C : IFoo, IBar
{
public string SomeMember { get; set; }
}
Three classes, A
implements IBar
, B
implements IFoo
and C
implements both. Now, take the following code:
SomeClass<A> aa = new SomeClass<A>(); // doesn't compile
SomeClass<B> bb = new SomeClass<B>(); // doesn't compile
SomeClass<C> cc = new SomeClass<C>(); // works fine
This means that we can do like so:
SomeClass<C> cc = new SomeClass<C>();
cc.FooBarMember = new C();
Think about what you're asking the compiler to do:
You're saying "This function returns an IBar or an IFoo." The compiler needs to know which because they have different interfaces. If you don't pick a specific one then it can't determine whether calls you make on the return value of that function are legitimate.
Tell me, given your code, is the following legal? (The answer is "who the heck knows"):
someClass.FooBarMember.FooMember;
The reason you can declare a type that implements both is that the compiler knows that you have both "FooMember" and "BarMember" declared in it, so adding:
IFooBar : IFoo, IBar{ <...> }
Means that any IFooBar
you declare can resolve calls to FooMember and BarMember correctly at compile time.
C# is not an interpreted language.
You can implementing multiple interfaces on a method using generics as follows:
public void SetMultipleInterfaceObject<T>(T item)
where T : IInterface1, IInterface2 {
_multipleInterfaceObject = item;
}
You can store the multiple interface object as follows. I'm not sure if there is a better way to do this. If there is, I haven't found it yet:
private object _multipleInterfaceObject;
public IInterface1? Interface1Object => (IInterface1) _compositeInterfaceObject;
public IInterface2? Interface2Object => (IInterface2) _compositeInterfaceObject;
精彩评论