首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在COM接口中包含非托管类型属性

在COM接口中包含非托管类型属性
EN

Stack Overflow用户
提问于 2015-02-28 14:04:14
回答 1查看 91关注 0票数 1

我正在用Oxygene为COM可调用的包装类编写COM接口。我不知道如何将System.Decimal属性作为货币(原生COM类型)来传递,因为Oxygene编译器不允许使用Delphi.NET的语法。

下面是我如何在Delphi.NET中成功地做到这一点的一个简单的例子

代码语言:javascript
复制
[ComVisible(True)]
IVarious = interface(IInterface)
['{2DBF9ACD-1574-4BE4-812A-5D0B8E9AA025}']
  function get_MyString: String;
  procedure set_MyString(strValue: String);
  [return: MarshalAs(UnmanagedType.Currency)]
  function get_MyAmount: System.Decimal;
  procedure set_MyAmount([MarshalAs(UnmanagedType.Currency)] curValue: System.Decimal);
  property MyString: String read get_MyString write set_MyString;
  property MyAmount: System.Decimal read get_MyAmount write set_MyAmount;
end;

// In Delphi.NET the implementing class only needed to have the methods, not the properties
[ComVisible(False)]
[ClassInterface(ClassInterfaceType.None)]
TVarious = class(TObject, Various)
private
  FMyString: string;
  FMyAmount: System.Decimal;
public
  function get_MyString: String;
  procedure set_MyString(strValue: String);
  [return: MarshalAs(UnmanagedType.Currency)]
  function get_MyAmount: System.Decimal;
  procedure set_MyAmount([MarshalAs(UnmanagedType.Currency)] curValue: System.Decimal);
end;

(我不想展示TVarious、getter和setter方法实现,因为它们非常明显。)

我知道Oxygene不需要接口来声明getter & setter方法。在我看来,它实际上不允许您有getter & setter方法。所以,我能把它转换成Oxygene (到目前为止)的唯一方法是:

代码语言:javascript
复制
[ComVisible(True)]
[Guid('2DBF9ACD-1574-4BE4-812A-5D0B8E9AA025')]
IVarious = interface
  property MyString: String read write;
  property MyAmount: System.Decimal read write;
end;

[ComVisible(False)]
[ClassInterface(ClassInterfaceType.None)]
TVarious = class(IVarious)
public
  property MyString: String;
  property MyCurrency: System.Decimal;
end;

但我的问题是,我还没有弄清楚如何将MarshalAs(UnmanagedType.Currency)属性包含到这些声明中。

有关如何在接口中包含此COM属性的帮助将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-02 09:25:04

你试过这个吗?

代码语言:javascript
复制
[ComVisible(True)]
[Guid('2DBF9ACD-1574-4BE4-812A-5D0B8E9AA025')]
IVarious = interface
  property MyString: String read write;
  property MyAmount: System.Decimal read write;
end;

[ComVisible(False)]
[ClassInterface(ClassInterfaceType.None)]
TVarious = class(IVarious)
public
  property MyString: String;
  [var: MarshalAs(UnmanagedType.Currency)]
  property MyAmount: System.Decimal;
end;

我没有办法测试这个,但你能看看它是否有效吗?

诚挚的问候,

耶伦

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28782595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档