0

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 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?

Thiago Padilha
  • 4,590
  • 5
  • 44
  • 69
  • 1
    Do you mean you want to create a class which *claims* to implement an interface but actually doesn't? – Jon Skeet May 12 '10 at 19:55
  • Yes, it would be the same as implementing an interface with empty method bodies, it claims to implement it but the methods dont do anything, then the CLR would throw an exception when that 'non-implemented' member was accessed(which would not happen when using the proxy) – Thiago Padilha May 12 '10 at 21:27

2 Answers2

1

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.

overslacked
  • 4,127
  • 24
  • 28
0

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.
  }
}
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150