C#C
C#3y ago
substitute

❔ Is it possible to use a generic type as another type outside of the generic class w/o reflection?

Hi, in C++ we can write code like the following:

#include<iostream>

class test1 {}; //example, can be anything.
class test2 {}; //example, can be anything.
class test3 {}; //example, can be anything.

template <typename T1, typename T2, typename T3>
class tuple
{
public:
    using First = T1;
    using Second = T2;
    using Third = T3;
};

using my_tuple = tuple<test1, test2, test3>; //Alias tuple<i,j,k>

int main(){
   my_tuple::First first_ele; //Variable of test1 derived from T1 on my_tuple alias
   my_tuple::Second second_ele; //Variable of test2 derived from T2 on my_tuple alias
   my_tuple::Third third_ele; //Variable of test3 derived from T3 on my_tuple alias

   printf("%s\n", typeid(first_ele).name()); //prints the type test1
   printf("%s\n", typeid(second_ele).name()); //prints the type test2
   printf("%s\n", typeid(third_ele).name()); //prints the type test3
}


This effectively allows a special type-of-types to exist that only exists as a way of tying together types into a singular type (tuple<i,j,k>) while allowing access to the inner types i,j,k at compile time.

I was wondering if there was an equivalent in C# that didn't require reflection.

In essence, I would like to do something like this
using CreateAccount = ReqResErr<Request.CreateAccount,Response.CreateAccount,Error.CreateAccount>

to later consume like
Envelope.Open<CreateAccount>

which does like
T1 Req = T0::First
T2 Res = T0::Second
T3 Err = T0::Third


Thanks!
Was this page helpful?