如何在CLR /托管C ++中实现变体类型?

时间:2021-11-01 19:49:36

In the .net CLR Object is the base for all class objects, but not basic types (e.g. int, float etc). How can I use basic types like Object? I.e. Like Boost.Variant?

在.net中,CLR Object是所有类对象的基础,但不是基本类型(例如int,float等)。如何使用像Object这样的基本类型?即像Boost.Variant一样?

E.g. like :-

例如。喜欢 :-

object intValue( int(27) );
if (intValue is Int32)
    ...

object varArray[3];
varArray[0] = float(3.141593);
varArray[1] = int(-1005);
varArray[2] = string("String");

4 个解决方案

#1


object, via boxing, is the effective (root) base-class of all .NET types. That should work fine - you just need to use is or GetType() to check the types...

对象,通过装箱,是所有.NET类型的有效(根)基类。这应该工作正常 - 你只需要使用is或GetType()来检查类型......

object[] varArray = new object[3];
varArray[0] = 3.141593F;
varArray[1] = -1005;
varArray[2] = "String";

#2


Since you mentioned you're in C++/CLI, you should be able to do:

既然你提到你是在C ++ / CLI中,你应该能够做到:

array<Object^>^ varArray =  gcnew array<Object^>(3);

varArray[0] = 3.141593;
varArray[1] = -1005;
varARray[2] = "String";

double val = *reinterpret_cast<double^>(varArray[0]);

#3


object varArray[3] = new object[3];
varArray[0] = 3.141593;
varArray[1] = -1005;
varArray[2] = "String";

#4


Thanks for the boxing answer. I need to box my return value, e.g.

谢谢你的拳击答案。我需要设置我的返回值,例如

    Object ^ createFromString(String ^ value)
    {
         Int32 i( Convert::ToInt32(value) );
         return static_cast< Object ^ >(i);
    }

I need to box the return value by casting to an Object pointer. Intuitive! :)

我需要通过强制转换为Object指针来包装返回值。直观! :)

And retrieve as:

并检索为:

    void writeValue(Object ^ value, BinaryWriter ^ strm)
    {
        Int32 i( *dynamic_cast< Int32 ^ >(value) );
        strm->Write(i);
    }

#1


object, via boxing, is the effective (root) base-class of all .NET types. That should work fine - you just need to use is or GetType() to check the types...

对象,通过装箱,是所有.NET类型的有效(根)基类。这应该工作正常 - 你只需要使用is或GetType()来检查类型......

object[] varArray = new object[3];
varArray[0] = 3.141593F;
varArray[1] = -1005;
varArray[2] = "String";

#2


Since you mentioned you're in C++/CLI, you should be able to do:

既然你提到你是在C ++ / CLI中,你应该能够做到:

array<Object^>^ varArray =  gcnew array<Object^>(3);

varArray[0] = 3.141593;
varArray[1] = -1005;
varARray[2] = "String";

double val = *reinterpret_cast<double^>(varArray[0]);

#3


object varArray[3] = new object[3];
varArray[0] = 3.141593;
varArray[1] = -1005;
varArray[2] = "String";

#4


Thanks for the boxing answer. I need to box my return value, e.g.

谢谢你的拳击答案。我需要设置我的返回值,例如

    Object ^ createFromString(String ^ value)
    {
         Int32 i( Convert::ToInt32(value) );
         return static_cast< Object ^ >(i);
    }

I need to box the return value by casting to an Object pointer. Intuitive! :)

我需要通过强制转换为Object指针来包装返回值。直观! :)

And retrieve as:

并检索为:

    void writeValue(Object ^ value, BinaryWriter ^ strm)
    {
        Int32 i( *dynamic_cast< Int32 ^ >(value) );
        strm->Write(i);
    }

相关文章