相关疑难解决方法(0)

Interop类型无法嵌入

我正在使用C#在.NET 4.0框架(beta2)上创建一个Web应用程序.

当我尝试使用名为"ActiveHomeScriptLib"的程序集时,出现以下错误:

无法嵌入Interop类型'ActiveHomeScriptLib.ActiveHomeClass'.请改用适用的界面.

当我将框架更改为3.5版时,我没有任何错误.

什么是互操作类型,为什么只有在我使用4.0框架时才会出现这种情况?

c# visual-studio-2010 visual-studio c#-4.0

648
推荐指数
7
解决办法
35万
查看次数

C#编译器如何检测COM类型?

编辑:我已将结果写成博客文章.


C#编译器有点神奇地处理COM类型.例如,这个陈述看起来很正常......

Word.Application app = new Word.Application();
Run Code Online (Sandbox Code Playgroud)

......直到你意识到这Application是一个界面.在接口上调用构造函数?Yoiks!这实际上被转换为对Type.GetTypeFromCLSID()另一个的调用Activator.CreateInstance.

此外,在C#4中,您可以对ref参数使用非ref 参数,并且编译器只是添加一个局部变量以通过引用传递,丢弃结果:

// FileName parameter is *really* a ref parameter
app.ActiveDocument.SaveAs(FileName: "test.doc");
Run Code Online (Sandbox Code Playgroud)

(是的,有一堆参数丢失.不是可选参数好吗?:)

我正在尝试调查编译器的行为,我没有假装第一部分.我可以做第二部分没有问题:

using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

[ComImport, GuidAttribute("00012345-0000-0000-0000-000000000011")]
public interface Dummy
{
    void Foo(ref int x);
}

class Test
{
    static void Main()
    {
        Dummy dummy = null;
        dummy.Foo(10);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想能够写:

Dummy dummy = new Dummy();
Run Code Online (Sandbox Code Playgroud)

虽然.显然它会在执行时爆炸,但没关系.我只是在试验.

编译器为链接的COM PIA(CompilerGeneratedTypeIdentifier)添加的其他属性似乎没有做到这一点......什么是神奇的酱油?

c# compiler-construction com c#-4.0

167
推荐指数
4
解决办法
2万
查看次数

如何初始化接口?

到目前为止,我学到的是我们无法创建接口的实例。

    interface IFood
    {
        string Color { get; set; }
    }
    class Apple : IFood
    {
        public string Color { get ; set; }
    }
Run Code Online (Sandbox Code Playgroud)

IFood food = new IFood(); //this gives compile error

IFood food = new Apple(); //this will work

到这里一切正常。但是当我和Microsoft.Office.Interop.Excel我一起工作时,我看到了类似下面的内容

Application excel = new Application();// Application is an interface

我在这里缺少什么?

Application接口的元数据

.net c# interface

6
推荐指数
1
解决办法
853
查看次数