Sim*_*mon 30 .net inheritance interface
有一些黑色的魔码在C#中,你可以定义一个接口的缺省实现.
所以你可以写
var instance = new ISomeInterface();
Run Code Online (Sandbox Code Playgroud)
有什么指针吗?
更新1:请注意,我没有问这是不是一个好主意.怎么可能这样做呢.
更新2:任何人看到接受的答案.
Dar*_*rov 33
黑魔法来了:
class Program
{
static void Main()
{
IFoo foo = new IFoo("black magic");
foo.Bar();
}
}
[ComImport]
[Guid("C8AEBD72-8CAF-43B0-8507-FAB55C937E8A")]
[CoClass(typeof(FooImpl))]
public interface IFoo
{
void Bar();
}
public class FooImpl : IFoo
{
private readonly string _text;
public FooImpl(string text)
{
_text = text;
}
public void Bar()
{
Console.WriteLine(_text);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您不仅可以实例化接口,还可以将参数传递给其构造函数:-)