c# - COM dll 的有效 .cs 文件

标签 c# .net dll

我是 Java 开发人员,但是 .Net 任务分配给我进行研发。我正在使用 Visual Studio 2008。我创建了示例 .cs 文件,如下所示...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example
{
    public class mathOperation
    {
        public int add(int a, int b)
        {
            return a + b;
        }

        public int subtract(int a, int b)
        {
            return a - b;
        }
    }
}

现在我想为这个.cs 文件生成的COM 操作注册.DLL 文件。我还设置了项目的属性 Register for COM interop。并 build 它。然后创建安装项目,但 .DLL 未注册 COM 操作。

如果此文件生成的 .DLL 没问题,那么有人可以向我提供有关如何创建安装项目以注册此类 dll 以进行 COM 操作的链接

我必须按照其他事情来创建可用于 COM.cs 文件

enter image description here

最佳答案

您必须添加接口(interface)并使用 GuidAttribute 修饰类型, ComVisibleClassInterfaceSystem.Runtime.Interopservices namespace 中找到.

确保以提升的方式运行 Visual Studio,因为互操作的注册需要对 HKEY_CLASSES_ROOT 的写入权限。

[ComVisible(true)]
[Guid("516FED99-YOUR-GUID-HERE-C9EA0177568A")]
public interface  IMathOperation
{
    int Add(int a, int b);
    int Subtract(int a, int b);
}

[ComVisible(true)]
[Guid("4354D883-YOUR-GUID-HERE-155A0DE30318")]
[ClassInterface(ClassInterfaceType.None)]
public class MathOperation:IMathOperation
{

    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Subtract(int a, int b)
    {
        return a - b;
    }
}

关于c# - COM dll 的有效 .cs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393820/

相关文章:

c++ - mysqlpp::Query::store() 上的 MySQL++ malloc_error_break

c# - 如何在C#中向对象数组添加值

java - 使用本地过程将数据插入数据库或使用最近的框架(如 spring 或 Entity Framework )将数据插入数据库之间有什么区别吗?

c# - 验证域名

.net - T4包含项目根目录中的文件路径

c# - 此 XAML 代码中的 IsEmpty 引用/成员在哪里?

python - Python 2 和 3 之间 ctypes 的差异

c# - 从 C# 调用旧的 OLE 组件

c# - WCF 和 Delphi - 我应该使用 bytes[] 还是字符串?

c# - C++ Interop 的局限性(隐式 PInvoke)