是否可以使已编译的类在运行时实现某个接口,例如:
public interface ISomeInterface {
void SomeMethod();
}
public class MyClass {
// this is the class which i want to implement ISomeInterface at runtime
}
这可能吗,如果是的话怎么办?
好吧,差不多了。您可以使用即兴界面。
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>();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)