© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
138 replies
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
}
#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>
tuple<i,j,k>
) while allowing access to the inner types
i,j,k
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>
using CreateAccount = ReqResErr<Request.CreateAccount,Response.CreateAccount,Error.CreateAccount>

to later consume like
Envelope.Open<CreateAccount>
Envelope.Open<CreateAccount>

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


Thanks!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ giving current class type to a generic class
C#CC# / help
3y ago
✅ Is it possible to convert a generic interface to a generic interface?
C#CC# / help
3y ago
Using pointers as generic a generic type
C#CC# / help
4y ago
use parameter from a class of type form in another form
C#CC# / help
2y ago