是否有可能使已编译的类在运行时实现某个接口,例如:
public interface ISomeInterface {
void SomeMethod();
}
public class MyClass {
// this is the class which i want to implement ISomeInterface at runtime
}
Run Code Online (Sandbox Code Playgroud)
这是可能的,如果是,那怎么样?
好吧差不多.您可以使用即兴界面.
https://github.com/ekonbenefits/impromptu-interface
来自https://github.com/ekonbenefits/impromptu-interface/wiki/UsageBasic的基本示例:
using ImpromptuInterface;
public interface ISimpleClassProps
{
string Prop1 { get; }
long Prop2 { get; }
Guid Prop3 { get; }
}
var tAnon = new {Prop1 = "Test", Prop2 = 42L, Prop3 = Guid.NewGuid()};
var tActsLike = tAnon.ActLike<ISimpleClassProps>();
Run Code Online (Sandbox Code Playgroud)