I think that is problematic. You introduce a relationship between two classes that are not related.
Consider the following code.
public interface IFoo { int MethodA(); int MethodB(); } public class Bar { int MethodA(); int MethodB(); } public class SomeClass { int MethodFoo(IFoo someFoo); }
Should it be legal?
int blah = someClass.MethodFoo((dynamic<IFoo>)bar);
It seems like this should be legal, because the compiler should be able to dynamically type the bar as something that implements IFoo.
However, at this point you are linking IFoo and Bar by calling in a completely separate part of your code.
If you are editing Bar because it no longer needs MethodB, suddenly someClass.MethodFood is no longer working, although Bar and IFoo are not connected.
In the same way, if you add MethodC () to IFoo, your code will break again, although IFoo and Bar are supposedly unrelated.
The fact is that although this would be useful in individual cases when there are similarities between objects that you do not control, there is a reason that interfaces must be explicitly bound to objects, and the reason is that the compiler may have the object implement it.
Devinb
source share