C# - Good way to expose Nullable<T> to COM

拈花ヽ惹草 提交于 2019-12-05 18:07:50

问题


We are working on exposing an assembly to 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.

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; }
}

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.

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


回答1:


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 Object to Variant



来源:https://stackoverflow.com/questions/1987261/c-sharp-good-way-to-expose-nullablet-to-com

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!