Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c# - How to make properties visible to COM in a .NET DLL (Methods DO work)

Solved, see comments!

I have a simple .NET DLL written in c#.

In asp-classic or VB.NET i can create the object and call a member function in the DLL without any problem. But, and this is my stumbling point, i can't access class properties.

Here's the sample code:

[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
    {
    }
}

Here's the server-side script:

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

I get the following error-message:

Microsoft VBScript Runtime Error "800a01b6'

Object Doesn't Support This Property or Method: MyProperty

Can anybody tell me, why i can call the function 'MyFunciton', but if i want to set the property-value, i get the error above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Properties must be included in the interface definition to make them visible to COM.

Example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...