gpt4 book ai didi

c# - 如何使属性对 .NET DLL 中的 COM 可见(方法确实有效)

转载 作者:行者123 更新时间:2023-12-05 04:13:32 25 4
gpt4 key购买 nike

Solved, see comments!

我有一个用 C# 编写的简单 .NET DLL。

在 asp-classic 或 VB.NET 中,我可以毫无问题地创建对象并调用 DLL 中的成员函数。但是,这是我的绊脚石,我无法访问类属性。

示例代码如下:

[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComEvents))]
public class Com : IComInterface
{
public string MyProperty{ get; set; } // <-- NOT ACCESSIBLE
public void MyFunction() // <-- ACCESSIBLE
{
}
}

这是服务器端脚本:

Set com = Server.CreateObject("ns.Com")    // WORKS
com.MyProperty = "abc" // GIVES ERROR
com.MyFunction // WORKS

我收到以下错误消息:

Microsoft VBScript Runtime Error "800a01b6'

Object Doesn't Support This Property or Method: MyProperty

谁能告诉我,为什么我可以调用函数“MyFunciton”,但是如果我想设置属性值,我会得到上面的错误?

最佳答案

属性必须包含在接口(interface)定义中以使其对 COM 可见。

例子:

[Guid("... some GUID ...")]
[ComVisible(true)]
public interface MyClassInterface
{
string MyProperty { get; set; }
bool MyMethod();
}

关于c# - 如何使属性对 .NET DLL 中的 COM 可见(方法确实有效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207659/

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