Creating a class for an interface at runtime, in C#

后端 未结 3 662
太阳男子
太阳男子 2021-02-14 11:08

I\'m looking at taking a set of objects, let\'s say there\'s 3 objects alive at the moment, which all implement a common interface, and then wrap those objects inside a fourth o

3条回答
  •  眼角桃花
    2021-02-14 11:45

    (I'm justifying an answer here by adding extra context/info)

    Yes, at the moment Reflection.Emit is the only way to solve this.

    In .NET 4.0, the Expression class has been extended to support both loops and statement blocks, so for single method usage, a compiled Expression would be a good idea. But even this won't support multi-method interfaces (just single-method delegates).

    Fortunately, I've done this previously; see How can I write a generic container class that implements a given interface in C#?

提交回复
热议问题