从一个网址我看到人们可以像这样实例化界面
class Program
{
static void Main(string[] args)
{
var foo = new IFoo(1);
foo.Do();
}
}
[
ComImport,
Guid("C906C002-B214-40d7-8941-F223868B39A5"),
CoClass(typeof(FooImpl))
]
public interface IFoo
{
void Do();
}
public class FooImpl : IFoo
{
private readonly int i;
public FooImpl(int i)
{
this.i = i;
}
public void Do()
{
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
如何写这样var foo = new IFoo(1);寻找指导.谢谢
这就是COM的工作原理.你已经宣布FooImpl是IFoo小学生.new IFoo(1);将被编译为new FooImpl(1);
根据C#规范的第17.5节,System.Runtime.InteropServices命名空间下的属性可能会破坏所有规则.这是Microsoft的C#实现特有的.
Marc Gravell和Jon Skeet有很好的博客文章:谁说你不能实例化界面?和伪造COM来欺骗C#编译器