提问人:JBL 提问时间:11/14/2023 最后编辑:JBL 更新时间:11/16/2023 访问量:49
CS1545 错误与特定的 COM 对象属性
CS1545 error with specific COM object properties
问:
我正在使用第三方库进行 COM 自动化。当尝试访问某些属性时,我需要使用而不是 .obj.get_Property()
obj.Property
这似乎只发生在同时具有 getter 和 setter 的字符串类型的属性上。
我想知道为什么 C# 无法处理这些情况,以及我是否可以做些什么来解决这个问题。
编辑 - 附加信息:
- 自动化目标应用程序附带了 .tlb 格式的类型库,Visual Studio 使用该库来生成互操作库。
- 感兴趣的反编译互操作库如下所示:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NS0;
namespace NS1
{
[ComImport]
[Guid("AAAAAAAA-BBBB-CCCC-DDDD-000000000000")]
[TypeLibType(1234)]
public interface SpecialDocument : Document
{
//...
[DispId(1234567890)]
new string Name
{
[MethodImpl(MethodImplOptions.InternalCall)]
[DispId(1234567890)]
[return: MarshalAs(UnmanagedType.BStr)]
get;
[MethodImpl(MethodImplOptions.InternalCall)]
[DispId(1234567890)]
[param: In]
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
//...
[DispId(1234567891)]
new string FullName
{
[MethodImpl(MethodImplOptions.InternalCall)]
[DispId(1234567891)]
[return: MarshalAs(UnmanagedType.BStr)]
get;
}
//...
[DispId(1234567892)]
new bool IsVisible
{
[MethodImpl(MethodImplOptions.InternalCall)]
[DispId(1234567892)]
get;
[MethodImpl(MethodImplOptions.InternalCall)]
[DispId(1234567892)]
[param: In]
set;
}
}
- 在上面的代码片段中,在定义的 3 个属性中,只有 Name 必须使用 get_Name 或 set_Name 进行访问。其他属性可以像在 C# 中预期的那样访问。
- Name 和 FullName 都继承自 NS0 中定义的父类 Document,其中它们具有相同的定义。
- 根据文档,产品的所有自动化接口都派生自 IDispatch。
编辑 - 添加重现步骤:
- 获取 FancySoft 的许可证并安装产品。
- 在 VS 2022 中启动新的 .NET 4.8 控制台项目。
- 导入 COM 引用。
- 在“Main”中,使用
Marshal.GetActiveObject("FancySoft.Application");
- 导航到 SpecialDocument 实例,并尝试读取文档的 Name 属性。
- CS1545系列
编辑 - 来自 的其他信息。TLB(使用 oleview.exe 提取):
// From "dispinterface Document":
[
uuid(AAAAAAAA-BBBB-CCCC-DDDD-111111111111),
helpcontext(0x00001111),
dual
]
dispinterface Document {
properties:
methods:
// ...
[id(0x76543210), propget, helpcontext(0x00001234)]
BSTR Name();
[id(0x76543210), propput, helpcontext(0x00001234)]
void Name([in] BSTR* rhs);
// ...
};
// From "interface Document":
[
odl,
uuid(AAAAAAAA-BBBB-CCCC-DDDD-111111111111),
helpcontext(0x00001111),
dual,
oleautomation
]
interface Document : BaseObject {
// No member description for the 'Name' property.
};
// From "dispinterface BaseObject":
[
uuid(AAAAAAAA-BBBB-CCCC-DDDD-222222222222),
helpcontext(0x00002222),
dual
]
dispinterface BaseObject {
properties:
methods:
// ...
[id(0x76543210), propget, helpcontext(0x00001234)]
BSTR Name();
[id(0x76543210), propput, helpcontext(0x00001234)]
void Name([in] BSTR* rhs);
// ...
};
// From "interface BaseObject":
[
odl,
uuid(AAAAAAAA-BBBB-CCCC-DDDD-222222222222),
helpcontext(0x00002222),
dual,
oleautomation
]
interface BaseObject : BaseDispatch {
// ...
[id(0x76543210), propget, helpcontext(0x00001234)]
HRESULT Name([out, retval] BSTR* oNameBSTR);
[id(0x76543210), propput, helpcontext(0x00001234)]
HRESULT Name([in] BSTR* oNameBSTR);
// ...
};
答: 暂无答案
评论
propput
BSTR*
BSTR
compatible
HRESULT Name([in] BSTR* oNameBSTR);
HRESULT Name([in] BSTR oNameBSTR);
Name {get;set;}