I have been struggling with this problem for a day and a half, hopefully someone can help me. Let's say I have structures like this in C#:
public struct Part
{
public double? x; // or System.Nullable<double> x, doesn't really matter
}
(these structures represent database tables, converted from Linq to SQL code created by SQLMetal)
I need to be able to expose these structures, containing nullable types, to COM so that they can be used in another application (C++). But I cannot figure out, for the life of me, how to do this. I thought I could create classes to encapsulate the nullable types:
public class NullableDouble
{
private double _Value;
// class methods...
}
public struct Part
{
public NullableDouble x;
}
That sort of works, but on the C++ side, I end up with a pointer to a class but no class definition (just an interface):
interface DECLSPEC_UUID("{E8EE4597-825D-3F4C-B20B-FD6E9026A51C}") _NullableDouble;
struct Part
{
MyDll_tlb::_NullableDouble* x;
}
Thus, I cannot dereference that pointer without a class definition from which to access the data members/methods on the C++ side. This still seems like a good option, if I can just figure out how to get the class definition in C++ (I'm new to COM).
I thought maybe I could use unsafe code, but I can't figure out how to convert from double? to double* (I'm new to C# too!):
unsafe public struct Part
{
public double* x;
}
Part part = new Part()
{
x = AnotherObject.x // AnotherObject.x is a System.Nullable<double>
}
I thought maybe I could use System.Variant, but C# doesn't like that either (inconsistent accessibility, whatever that means).
public struct Part
{
public Variant x;
// produces 2 errors:
// 1) System.Variant inaccessible,
// 2) inconsistent accessibility
}
I've been using C++ for 20 years, but I am fairly new to COM and C#, so this is a bit of a struggle for me.
Worst-case scenario...I'll just create a boolean member in the struct for each of the nullable types and use that to indicate whether the value is to be treated as if it is null. But that just seems stupid. Surely there must be some way to expose nullable types via COM. Why would Microsoft create .NET types that can't be used in COM? Those guys in Redmond aren't idiots (although sometimes it sure seems like it).
So, what are my options? What is the best way to do this?
Thanks in advance.