问题描述
我正在一个需要将C ++代码转换为C#的项目中。
I am working on a project where I need to convert C++ code to C#.
我遇到了 _bstr_t
在代码中,我想知道C#中的等效方式。
I came across _bstr_t
in the code I would like to know how the equivalent in C#.
推荐答案
是。这是中的字符串,该字符串已跨语言标准化。
The _bstr_t
class is a wrapper for the BSTR
type. This is a string in OLE Automation that is standardized across languages.
也就是说,.NET中的等效类型是。
That said, the equivalent type in .NET is a System.String
.
关键是与之交互(或定义接口用于.NET代码中的COM互操作),则需要使用,其值来自,例如所以:
The key is when interacting with it (or defining your interfaces for COM interop in .NET code), you'll want to use the MarshalAsAttribute
with a value from the UnmanagedType
enumeration of UnmanagedType.BStr
, like so:
// This is on an interface that is in unmanaged code.
public void DoSomething([MarshalAs(UnmanagedType.BStr] string myString);
如果您的课程实际上是在COM接口中公开 _bstr_t
,则应更改它以公开 BSTR
; _bstr_t
是一个帮助程序类,不能跨越接口边界公开。 BSTR
用于该类和方法在 _bstr_t
上用于处理 BSTR
实例的分配和使用。
Note that if your class is actually exposing the _bstr_t
in a COM interface, then you should change it to expose a BSTR
; _bstr_t
is a helper class that isn't meant to be exposed across interface boundaries. The BSTR
is for that and the methods on _bstr_t
are for handling the allocation and use of BSTR
instances.
这篇关于在C#中等效于_bstr_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!