Is there any type-safe, compile-time checked possibilty of referring to values that implement multiple interfaces?
Given
interface A {
void DoA();
}
interface B {
void DoB();
}
I'm able to write code for objects implementing A or B, but not both. So I've to come up with ugly wrappers:
class ABCollection {
private class ABWrapper : A, B {
private readonly A a;
private readonly B b;
public static ABWrapper Create<T>(T x) where T : A, B {
return new ABWrapper { a = x, b = x };
}
public void DoA() {
a.DoA();
}
public void DoB() {
b.DoB();
}
}
private List<ABWrapper> data = new List<ABWrapper>();
public void Add<T>(T val) where T : A, B {
data.Add(ABWrapper.Create(val));
}
}
Is there a trick to write this code more intuitively without losing type-safety (runtime-casts etc.)?
E.g.
private List<A and B> ...
Edit: This is not about having a list in particular - I just wanted to give a "complete" example with the issue of storing such values. My problem is just how to type a combination of both interfaces (like A & B or A and B).
Another more useful example: List<IDrawable & IMovable> ...
You can do parametric polymorphism like that in C#, but not subtype polymorphism. That is, you can create a polymorphic method like:
void Foo<T>(T t) where T : IFoo, IBar
{
t.Foo();
t.Bar();
}
and then you must pass an object whose type is known at compile time to implement both IFoo and IBar.
But there is no way to say
void Foo(IFoo-and-IBar t)
{
t.Foo();
t.Bar();
}
and then pass in a value that is both an IFoo and an IBar. Neat feature, but not one we support.
Well, as Eric Lippert said, there's no IFoo-and-IBar type you can use as a method parameter type.
However, I was playing around with some ideas and came up with an alternate way of using your wrapper class that may be better. I'll leave that up to you (or whoever else might search for this question) to decide:
CLASSES
public abstract class ABWrapper : IA, IB
{
private readonly IA a;
private readonly IB b;
protected ABWrapper( IA a, IB b ) { this.a = a; this.b = b; }
// Implement methods on IA and IB
}
public sealed class ABWrapper<T> : ABWrapper
where T : IA, IB
{
private ABWrapper( T a, T b ) : base( a, b ) { }
public static implicit operator ABWrapper<T>( T t )
{
if ( t == null ) return null;
return new ABWrapper<T>( t, t );
}
}
EXAMPLE
public class AB : IA, IB { }
void Method( ABWrapper x )
{
}
void Main()
{
AB x = null;
Method( (ABWrapper<AB>) x );
}
The icky thing about this is that you need to do a cast to ABWrapper<T> at every call site. You could also create an extension method ABWrapper ToABWrapper<T>(this T t) where T : IA, IB to replace the cast if that would be more preferable.
It would be cool if the compiler could reason that an implicit conversion from AB to ABWrapper exists via implicit conversions to and from ABWrapper<T>. There's probably a very good reason it doesn't try to do that, however.
However, what you gain is the ability to put ABWrapper all throughout your method parameters without needing to genercize the methods.
I'm not clear on why you'd want to do this. If you did, you could declare a base interface:
interface AorB {}
interface A : AorB {
void DoA();
}
interface B : AorB {
void DoB();
}
and store those in the collection. Of course you'd have to is- or as-cast when retrieving (standard extension methods could help here).
It seems to me that this is a possible violation of SRP, and the collection is doing too much. Alternately the interfaces are too finely-grained.
Related
I'm trying to do the following:
public class A
{
}
public class B
{
}
Somewhere along the project I want to have this:
public class C
{
public T func<T>(T obj) [where T can be either of class A or class B]
{
obj.x = 100;
return obj;
}
}
I've been trying:
public T func<T>(T obj) where T: A, B
but this gives me:
The type class constraint 'B' must come before any other constraint.
Can someone explain me how to make func accept only class A or class B?
Exactly as it's described in the question, this job is better handled by overload resolution:
public class C
{
public A func(A obj)
{
obj.x = 100;
return obj;
}
public B func(B obj)
{
obj.x = 100;
return obj;
}
}
But I understand that A and B may be placeholders for any number of types, and it could get tedious to account for them all. In that case, you'll need a common interface that's supported by each of your classes:
interface IBase
{
int x;
}
public class C
{
public IBase func(IBase obj)
{
obj.x = 100;
return obj;
}
}
Note that at this point we still have no need of generics. Additionally, you may need to support a number of types that won't all fit together under a common interface. In this case, still build the interface and put as many types with that interface as possible. If needed, build another interface for a few more types ... and so on... and then between interfaces and specific types you can handle things with overload resolution.
You need some kind of common base for both the classes, either have them implement the same interface as the below code or have them inherit from same class. You can not have a generic constrained to 2 types.
public interface IFooBar
{
void DoThis();
}
public class Foo : IFooBar
{
public void DoThis()
{
//Do something
}
}
public class Bar : IFooBar
{
public void DoThis()
{
//Do something
}
}
public class C
{
public T func<T>(T obj) where T : IFooBar
{
obj.DoThis();
return obj;
}
}
The generics classes are just like any other class, you can't (and shouldn't) have multiple inheritance of classes, you can inherit one class and multiple interfaces.
in your case you should apply an interface on both classes and restrict the generics on that interface.
you can see some documentation in:
Constraints on Type Parameters (C# Programming Guide)
interface IMarkerInterface{} // there is a such pattern called marker
// interface . No need to have methods if there
// is no need for, A and B can just implement it
public class A: IMarkerInterface
{
}
public class B: IMarkerInterface
{
}
public class C
{
public T func<T>(T obj).Where T:IMarkerInterface
{
obj.x = 100;
return obj;
}
}
public T func<T>(T obj) where T: A, B this means T should extend both A and B , but multiple inheritance is not valid in C# ,so it won't work.
You could do one of the following though :
you could make A and B have a common parent via an interface or an abstract class , but that would be code modification.
since both A and B have a default no-arg constructor you could use where T: new().
Also, you can not do obj.x = 100; as there is no way to guarantee thatT will have a instance variable x.
In the example below, is there a way for a method of the implementing class to explicitly tell the compiler which interface member it implements?
I know it's possible to resolve ambiguity between interfaces, but here it is within one interface.
interface IFoo<A,B>
{
void Bar(A a);
void Bar(B b);
}
class Foo : IFoo<string, string>
{
public void Bar(string a) { }
public void Bar(string b) { } // ambiguous signature
}
I don't think that you can solve that directly by only using one interface because the method signatures may unify for some cases.
If you realy need this feature I think you've to introduce a new interface that will be derived by foo.
public interface IBar<T>
{
void Bar(T t);
}
public interface IFoo<A, B> : IBar<A>
{
void Bar(B b);
}
Now you're able to explicitly implement both interfaces:
public class Foo : IFoo<string, string>
{
void IFoo<string, string>.Bar(string b)
{
Console.WriteLine("IFoo<string, string>.Bar: " + b);
}
void IBar<string>.Bar(string t)
{
Console.WriteLine("IBar<string>.Bar: " + t);
}
}
But if you want to use it you've to cast your instance to the special interface:
var foo = new Foo();
((IFoo<string, string>)foo).Bar("Hello");
((IBar<string>foo).Bar("World");
This prints as expected:
IFoo<string, string>.Bar: Hello
IBar<string>.Bar: World
Hopefully this will help you. I don't think that there is another way of doing that.
It is possible to implement only one interface explicitly so that you only need to cast if you want to call the other method.
You just need to remove the duplicate line:
interface IFoo<A, B>
{
void Bar(A a);
void Bar(B b);
}
class Foo : IFoo<string, string>
{
public void Bar(string a) { }
}
In this case having a single implementation of void Bar(string a) implements both methods of the interface.
Actually calling the interfaces is much harder though. You need reflection.
You can't do that.
Bar() will be ambiguous in this situation. Consider changing the interface methods to BarA(), BarB().
Also, consider making A and B more meaningful names (e.g., IFoo<TKey, TValue>), then your methods could be BarKey() and BarValue().
Here is the situation. In some cases I find myself wanting a class, let's call it class C that has the same functionalities as class A, but with the addition that it has interface B implemented. For now I do it like this:
class C : A,B
{
//code that implements interface B, and nothing else
}
The problem will come if class A happens to be sealed. Is there a way I can make class A implement interface B without having to define class C (with extension methods or something)
Basically: no. That is part of what "mixins" could bring to the table, but the C# languauge doesn't currently support that (it has been discussed a few times, IIRC).
You will have to use your current approach, or (more commonly) just a pass-through decorator that encapsulates A rather than inheriting A.
class C : IB
{
private readonly A a;
public C(A a) {
if(a == null) throw new ArgumentNullException("a");
this.a = a;
}
// methods of IB:
public int Foo() { return a.SomeMethod(); }
void IB.Bar() { a.SomeOtherMethod(); }
}
The only way I see, is to change inheritance to aggregation, like this:
class C : B
{
public C(A instanceToWrap)
{
this.innerA = instanceToWrap;
}
//coda that implements B
private A innerA;
}
There seems to be a possibility to inject interface in run-time, as it is done with Array class and IEnumerable<T> interface, but it seems a bit of an overkill.
Is there a way I can make class A implement interface B without having to define class C (with extension methods or something)
The short answer is no. You can't make A implement B because you don't have control of A. However, I think you're headed down the right road with extension methods. Consider this:
public static class AImplementsBExtensions
{
public static void Func1(this A o) { }
public static void Func2(this A o) { }
}
Now clearly I have no idea what methods exist on B, but this is how you can implement B on A when you can't inherit from it.
Bear in mind, this is not an implementation. If you add or remove methods from the interface you'll have to do that by hand here. But now you can do this:
var a = new A();
a.Func1();
You could try creating your class without the inheritance: class C : B and as a wrapper around A.
Additionally you can provide implicit conversion operators so that code like A obj = new C(new A()) would work similar to how a derived class would work.
class C : B
{
private A _inner;
public C(A inner)
{
this._inner = inner;
}
public A Inner { get { return this._inner; } }
public static implicit operator A(C obj)
{
return obj == null ? (A)null : obj._inner;
}
public static implicit operator C(A obj)
{
return new C(obj);
}
}
I have a simple c# question (so I believe). I'm a beginner with the language and I ran into a problem regarding interfaces and classes that implement them. The problem is
I have the Interface iA
interface iA
{
bool method1
bool method2
bool method3
}
and 3 classes that implement the interface: class B, C and D
class B : iA
{
public bool method1
public bool method2
public bool method3
}
if class B had another method that is not in the interface, let's say method4() and I have the following:
iA element = new B();
and then I would use :
element.method4();
I would get an error saying that I don't have a method4() that takes a first argument of type iA.
The question is: Can I have an object of interface type and instantiated with a class and have that object call a method from the class, a method that is not in the interface ?
A solution I came up with was to use an abstract class between the interface and the derived classes, but IMO that would put the interface out of scope. In my design I would like to use only the interface and the derived classes.
Yes, that is possible. You just need to cast the Interface type to the class type like this:
iA element = new B();
((B)element).method4();
As suggested by wudzik, you should check if elemnt is of the correct type:
if(element is B)
{
((B)element).method4();
}
You have to cast the interface type to the class type; usually we do it by means of as:
B b = element as B; // <- try cast element as B
if (!Object.RefernceEquals(null, b)) { // <- element is B or can be legaly interpreted as B
b.method4();
}
The advantage of "as" is that there's only one cast operation, while "is" and (B) have to do two casts.
Without casting you could do this.
interface iA
{
bool method1();
bool method2();
bool method3();
}
interface IFoo : iA
{
bool method4();
}
class B : IFoo
{
public bool method1() {}
public bool method2() {}
public bool method3() {}
public bool method4() {}
}
IFoo element = new B();
element.method4();
NB: Try to use capital I prefix for C# interfaces.
public interface IA
{
void DoSomething();
void Calculate();
}
public interface IB
{
void DoSomethingElse();
void Calculate();
}
public class A : IA
{
void DoSomething() { }
void Calculate() {}
}
public class B : IB
{
void DoSomethingElse() { }
void Calculate() {}
}
public class C : IA, IB
{
//How can I implement Calculate() in class B and DoSomething() in class A?
}
How can I avoid duplicate code in class C. Reference: How to simulate multiple inheritance in C#. I don't want to write the full methods again in class C. Thanks for any help.
Assuming that IA.Calculate() is not the same as IB.Calculate() and you therefore can't just make IB inherit from IA, you can implement both interfaces in C by delegating execution on private instances of A and B:
public class C : IA, IB
{
private A _a;
private B _b;
public C()
{
this._a = new A();
this._b = new B();
}
public void DoSomething()
{
this._a.DoSomething();
}
void IA.Calculate()
{
this._a.Calculate();
}
public void DoSomethingElse()
{
this._b.DoSomethingElse();
}
void IB.Calculate()
{
this._b.Calculate();
}
}
Note that you are not achieving multiple inheritance at all. C# has singular inheritance. You are implementing multiple interfaces.
There is absolutely no reason why you cannot implement the interfaces in classes A and B, and then have B derive from A and C derive from B. The method implementations from the interfaces can even be abstract, which forces the child class to do the implementation. If you follow this approach then you can still pass C around as IA or IB simply by casting or using the as keyword.
You can also have interface IB "inherit"1 (implement) IA, which means anything that implements IB must also implement IA.
1 When IB derives from IA it isn't inheritance like it is at the class level, it is actually still implementation. So IB implements IA, it doesn't inherit it.