Domanda

Nel codice C ++ / CLI devo verificare se il tipo è un tipo generico specifico.In C # sarebbe:

public static class type_helper {
    public static bool is_dict( Type t ) {
        return t.IsGenericType
            && t.GetGenericTypeDefinition() == typeof(IDictionary<,>);
    }
}

ma in cpp ++ \ cli non funziona allo stesso modo, il compilatore mostra l'errore di sintassi:

class type_helper {
public:
    static bool is_dict( Type^ t ) {
        return t->IsGenericType && t->GetGenericTypeDefinition()
            == System::Collections::Generic::IDictionary<,>::typeid;
    }
};

Il modo migliore che trovo è confrontare stringhe come questa:

class type_helper {
public:
    static bool is_dict( Type^ t ) {
        return t->IsGenericType
            && t->GetGenericTypeDefinition()->Name == "IDictionary`2";
    }
};

Qualcuno conosce il modo migliore?

PS: È una limitazione di typeof (typeid) in c ++ \ cli o non conosco il systax "corretto"?

È stato utile?

Soluzione

Potresti scrivere:

return t->IsGenericType
    && t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top