I would like to define a type implementing a certain interface, however I would only implement it in a proxy at runtime. I can see two obstacles in this scenario :
1-Make the compiler ignore non implemented interfaces. 2-Make the CLR ignore(or开发者_JS百科 at least delay) the TypeLoadException with the following description : "Method SOMEMETHOD in type SOMETYPE from assembly SOMEASSEMBLY does not have an implementation."
Is something like this possible?
If I needed to do this, I would create a class that provided some kind of basic/empty implementation of the interface to make the compiler happy, and then descend off that class to provide the actual implementation.
Any other way I would consider just too hackish - I wouldn't feel comfortable that whatever behavior I was exploiting would not be changed/fixed in the future.
If your type inherits an interface then it must implement every member from it. The closest thing I can of think of that you could do is to throw a NotImplementedException.
public interface IFoo
{
void DoSomething();
}
public class Foo : IFoo
{
void IFoo.DoSomething()
{
throw new NotImplementedException();
}
}
public class FooProxy : Foo
{
public void DoSomething()
{
// Do something meaningful here.
}
}
精彩评论