简体   繁体   English

如何将对象转换为C ++ / CLI中不知道其类型参数的通用列表?

[英]How can I cast an Object to a generic List I don't know the type parameter of in C++/CLI?

I'm writing glue code between MATLAB and C# (I don't have access to the one sold by MATLAB). 我正在MATLAB和C#之间编写粘合代码(我无法访问MATLAB出售的代码)。

I want to map List<T> to a particular MATLAB structure (array of structs). 我想将List<T>映射到特定的MATLAB结构(结构数组)。 I have the following code in my conversion function. 我的转换函数中有以下代码。

Casting an Object (which is actually a List<MyClass> ) to a List<Object> throws an InvalidCastException. Object (实际上是List<MyClass> )强制转换为List<Object>会引发InvalidCastException。

    // o is an C# Object I want to convert
    System::Type^ t = o->GetType();
    System::Type^ GenericListType = System::Collections::Generic::List<int>::typeid->GetGenericTypeDefinition();
    System::Type^ gt = t->IsGenericType ? t->GetGenericTypeDefinition() : nullptr;

    // ...

    // List<T> check
    else if (gt == GenericListType) {
        // map List<struct/class> to matlab struct array (export public fields only)
        // using actual matrix (dims != 1x1)
        // this save space as the fields name are stored only once

        // I can't cast to an explictit List<T> because T can be any class
        // but I need to know the list size

        // XXX: throws InvalidCastExceptions
        auto olist = (System::Collections::Generic::List<System::Object^>^)o; 
        const int count = olist->Count;

        if (count == 0) {
            System::Console::WriteLine("from_c_to_ml(): empty List<T>, returning nullptr, crash probably imminent, farewell my friends...");
            return nullptr;
        }

        // Now we need the actual type T (of List<T>) to know the public fields
        auto tlist = olist[0]->GetType();
        array<System::Reflection::FieldInfo^>^ fields = tlist->GetFields();
        const int fieldnb = fields->Length;

        // do stuff with it...
    }

强制转换为非通用List类型:

auto olist = (System::Collections::IList^)o;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何投放清单 <Object> 列出 <Foo> 当foo是类型变量并且我不知道类型时 - How can I cast List<Object> to List<Foo> when foo is a type variable and I don't know the type 我可以在C#中将对象强制转换为其通用类型吗? - can I cast an object to it's generic type in C#? c# - 如何将具有`object`类型的变量转换为具有泛型类型的类? - How can I cast a variable with the `object` type to class with generic type in c#? 我可以按类型转换通用列表吗? - Can I Cast a Generic List by Type? 为什么不能将`this`转换为泛型? - Why can't I cast `this` to a generic type? 如何比较两个 IEnumerable<T> 在 C# 中,如果我不知道实际的对象类型? - How to compare two IEnumerable<T> in C# if I don't know the actual object type? 如果我不知道正确的类型,如何反序列化对象? - How can I deserialize an object if I don't know the right type? 我不知道的演员类型是什么 - Cast type which i don't know yet what is it C#泛型如何获取没有对象类型作为参数的泛型方法的参数类型? - C# Generics How Can I get the type passed as an argument for a generic method with no object type as a parameter? 当我所知道的是集合的类型时,如何在ac #Generic集合中找到T的类型? - How can I find the type of T in a c# Generic collection of T when all I know is the type of the collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM