gpt4 book ai didi

c# - 在运行时实现接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:57 26 4
gpt4 key购买 nike

是否可以让一个已经编译好的类在运行时实现某个接口(interface),一个例子:

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>();

关于c# - 在运行时实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16802712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com