C# - 良好的方式来揭露可空< T>对COM良好、方式、LT、GT

2023-09-03 06:28:41 作者:誰與我狂

我们正在组装暴露于COM。

We are working on exposing an assembly to COM.

在其他方面,我们使用频率空的值,如长时间?日期时间?等等,这些都是通用的类型,不能向COM公开。

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?

我们已经尝试了以下内容:

We have tried the following:

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

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

现在的问题是,我们需要一种方式来传递空来回通过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)?如果声明成员为对象,你应该能够传递一个日期时间或空和COM互操作应该处理就OK了。请参阅下面的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.

Marshaling对象变异