At what point do you say WTF?! Runtime addition of methods to class in C#
time to read 4 min | 682 words
Assume that I have this piece of code:
public interface InterfaceWithExplicitImpl<T> { IEnumerator<T> GetEnum1(); }
Now, check the picture. This is 100% repreducable, but only under very specific set of scenarios (basically, running System.Reflection.Emit stuff). I am not sure how exactly I am supposed to handle this sort of an issue.
Here is the code to repreduce the issue:
You need to reference Dynamic Proxy 2, but this code produce stuff that I would bet is flat out impossible.
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: Console.WriteLine("Before it has {0} methods", typeof(MyInterfaceWithExplicitImpl<int>).GetMethods().Length);
6:
7: ProxyGenerator generator = new ProxyGenerator(new PersistentProxyBuilder());
8: generator.CreateInterfaceProxyWithoutTarget(typeof(MyInterfaceWithExplicitImpl<int>), new StandardInterceptor());
9: generator = new ProxyGenerator(new PersistentProxyBuilder());
10: generator.CreateInterfaceProxyWithoutTarget(typeof(MyGenericInterface<object>),
11: new Type[] { typeof(MyInterfaceWithExplicitImpl<int>) },
12: new StandardInterceptor());
13:
14:
15: Console.WriteLine("After it has {0} methods", typeof(MyInterfaceWithExplicitImpl<int>).GetMethods().Length);
16: }
17: }
18:
19: public interface MyInterfaceWithExplicitImpl<T>
20: {
21: IEnumerator<T> GetEnum1();
22: }
23:
24: public interface MyGenericInterface<T> where T : new()
25: {
26: T DoSomething(T t);
27: }
Comments
That's wild. What are the methods defined on the interface that are picked up in the debugger?
That is not even the debugger's fault.
Run the code I have there, it will print 1 method and the 2 methods!
Comment preview