简体   繁体   English

COM Interop中的.NET属性?

[英].NET Properties in COM Interop?

Is it true that you cannot use COM Interop to expose COM properties? 是不是您不能使用COM Interop公开COM属性? Everything has to be a method? 一切都必须是一种方法吗?

If this is not true, how do you do it? 如果不是这样,你怎么做?

Not true. 不对。

I understand your question to be asking about COM calling or using a .NET class . 我理解你的问题是要求COM调用或使用.NET类

According to this page , you can expose methods, properties and fields of managed classes to COM consumers. 根据此页面 ,您可以向COM使用者公开托管类的方法,属性和字段。

All managed types, methods, properties, fields, and events that you want to expose to COM must be public. 要向COM公开的所有托管类型,方法,属性,字段和事件都必须是公共的。 Types must have a public default constructor, which is the only constructor that can be invoked through COM. 类型必须具有公共默认构造函数,这是唯一可以通过COM调用的构造函数。

Does this work for you?: 这对你有用吗?:

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("A1209192-723B-4325-8599-FB39D9F202D9")]
public interface ITicklee
{
    [DispId(1)]
    void TickleMe();

    [DispId(2)]
    int Count{get;}
}


[Guid("45715A3B-CA95-49f7-9889-A0022B31EF8A")]
public class Elmo : ITicklee
{
    // default ctor
    public Elmo () {}

    private int _tickleCount;

    public int Count
    {
        get
        {
            return _tickleCount;
        }
    }

    public void TickleMe()
    {
        _tickleCount++;
    }
}

A VBScript test client might look like this: VBScript测试客户端可能如下所示:

Sub TestTickle()  

    WScript.echo("")

    WScript.echo("Instantiating an Elmo ...")
    dim elmo
    set elmo = WScript.CreateObject("Ionic.Tests.Com.Elmo")

    WScript.echo("Tickling...")

    For i = 1 to 5
      elmo.TickleMe()
    Next  

    WScript.echo("")
    c = elmo.Count

    WScript.echo("Tickle Count = " & c)

    ' one for the road'
    elmo.TickleMe()

End Sub

call TestTickle()     ' ahem '

If you mean that you cannot expose com object properties to a .NET application using interop, and the COM object was created in VB, then you are correct. 如果您的意思是您不能使用interop将com对象属性公开给.NET应用程序,并且COM对象是在VB中创建的,那么您是正确的。 VB uses property LET for native types and property SET for object types. VB对本机类型使用属性LET,对对象类型使用属性SET。 If you try to set a property of your COM object from your .NET application, it tries to use SET. 如果您尝试从.NET应用程序设置COM对象的属性,它会尝试使用SET。 If the underlying property is a native VB type, this fails. 如果底层属性是本机VB类型,则会失败。

As far as I know, the only solution aside from using methods rather than properties is to manually manipulate the IDL. 据我所知,除了使用方法而不是属性之外,唯一的解决方案是手动操作IDL。

评论:afaik你只能使用只读属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM