C# - 很好的暴露Nullable< T>到COM [英] C# - Good way to expose Nullable<T> to COM

查看:224
本文介绍了C# - 很好的暴露Nullable< T>到COM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在努力将一个程序集暴露给COM。

We are working on exposing an assembly to COM.

除此之外,我们还使用可空值,例如long?DateTime?

Among other things, we frequency use nullable values such as long?, DateTime?, etc. These are generic types and can't be exposed to COM.

什么是COM的这些数据类型的好替代品?

What is a good substitute for these data types for COM?

我们尝试了以下操作:

//Original CustomerID property in class
public long? CustomerID
{
   get;
   set;
}

//Explicit COM interface
long IComInterface.CustomerID
{
  get { return CustomerID.GetValueOrDefault(); }
  set { CustomerID = value; }
}

问题是,我们需要一个方法来传递null通过COM。使用一个像-1或0的数字不会工作,因为这些都是有效的值。

The problem is, we need a way to pass "null" back and forth through COM. Using a number like -1 or 0 won't work because these are valid values as well.

我们必须使用nullables b / c这些最初来自我们的数据库模式。

We are having to use nullables b/c these originally came from our database schema.

推荐答案

在COM端使用Variant(VT_DATE)如果你声明的成员作为对象,你应该能够传递一个DateTime或null和COM互操作应该处理它确定。有关详细信息,请参阅MSDN上的以下内容。

What about using a Variant (VT_DATE) on the COM side? If you declare the member as object, you should be able to pass a DateTime or null and COM interop should handle it ok. See the following on MSDN for more details.

将对象转换为变体

这篇关于C# - 很好的暴露Nullable< T>到COM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆