首页 > 解决方案 > 使用 C# 中的 COM 属性时出现错误 CS1545

问题描述

我创建了具有读/写属性的 COM 接口:

[
    object,
    uuid(...),
    dual,
    pointer_default(unique)
]
IInterfaceWithProperty : IDispatch
{
     [propget, id(1)] HRESULT Property([out, retval] IInterface2** ppObject); 
     [propput, id(1)] HRESULT Property([in] IInterface* pObject); 
};

当尝试在 C# 中使用时:

var value = object.Property;
object.Property = value;

收到以下错误:

error CS1545: Property, indexer, or event 'IInterfaceWithProperty .Property' is not supported 
by the language; try directly calling accessor methods 
'IInterfaceWithProperty.get_Property()' or 
'IInterfaceWithProperty.set_Property(IInterface)'

可能是什么原因?

标签: c#comcom-interop

解决方案


错误的原因是属性在 getter 和 setter 中应该具有相同的类型。

将 IInterfaceWithProperty 更改为

[
    object,
    uuid(...),
    dual,
    pointer_default(unique)
]
IInterfaceWithProperty : IDispatch
{
     [propget, id(1)] HRESULT Property([out, retval] IInterface2** ppObject); 
     [propput, id(1)] HRESULT Property([in] IInterface2* pObject); 
};

问题消失了。


推荐阅读