Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:22:43+00:00 2026-05-30T10:22:43+00:00

Suppose I have an interface: IFoo<T1,T2> and a class Moo<T1> : IFoo<List<T1>, Stack<T1>> .

  • 0

Suppose I have an interface: IFoo<T1,T2> and a class Moo<T1> : IFoo<List<T1>, Stack<T1>>.

At runtime I can call: typeof(Moo<>).GetInterfaces() and it gives me an array with one type in it, so I guess that in some sense the type typeof(IFoo<List<>, Stack<>>) can be created at runtime. However, that syntax does not work.

What is the correct syntax to define that type at runtime?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:22:45+00:00Added an answer on May 30, 2026 at 10:22 am

    I guess that in some sense the type typeof(IFoo<List<>, Stack<>>) can be created at runtime.

    Though that statement is true, your logic is not sound because the true conclusion does not follow logically from the premise you’ve stated. The type returned by

    typeof(Moo<>).GetInterfaces()[0]
    

    is not IFoo<T1, T2> constructed with List<> and Stack<> in the first place.

    Rather, it is IFoo<T1, T2> constructed with List<T1> and Stack<T1>, where T1 is the T1 of Moo<T1>, not the T1 of IFoo<T1, T2>. When making sample code to discuss type construction, I strongly discourage you from making type parameters that are logically different but have the same name. It is very confusing.

    In case that is not clear, let’s take a step back. Every generic type has an associated type called its instance type, which is the generic type constructed with its own type parameters. That is, when you say:

    class C<T>
    {
        Type type1 = typeof(C<T>);
        Type type2 = typeof(C<>);
    }
    

    From the compiler’s perspective, the types C<> and C<T> are the same type. But these will be different type objects at runtime because of course at runtime there will never be an instance of C<T>, only C<int> or C<string> or whatever. The first typeof syntax gives you the type as it is constructed at runtime. The second gives you the instance type from the perspective of the compiler.

    So let’s consider your case in more detail. Suppose you have:

    interface I<T, U> {}
    class C<V> {}
    class D<W> {}
    class E<X> : I<C<X>, D<X>> {}
    

    then typeof(E<>).GetInterfaces()[0] is the type as it is known to the compiler, namely, I<C<X>, D<X>>. But I<C<>, D<>> is a completely different type; that is I<C<V>, D<W>> which is not at all the same type from the compiler’s perspective.

    What is the correct syntax to define that type at runtime?

    There is none.

    There is no C# typeof syntax that produces the type “generic type constructed with type arguments that are the instance types of other generic types”. That would be:

    typeof(I<,>).MakeGenericType(typeof(C<>), typeof(D<>)) 
    

    To give you I<C<V>, D<W>>.

    Nor is there a typeof syntax for “generic type constructed with the type parameters of a different generic type”. kvb’s answer gives the Reflection code needed to build I<C<X>, D<X>>.

    I am curious as to why you would want to construct such a bizarre type in the first place. What’s your application?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this annotation class @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Suppose we have some class A that implements some interface I I i =
Suppose I have Objective C interface SomeClass which has a class method called someMethod
Suppose I have a class that implements an interface: public class A implements IB
Suppose I have: interface Foo { void doStuff(); } class FooImpl implements Foo {
Suppose I have an empty interface class IBaseInterface which is used only to label
Suppose I have a parent class that implements a protocol: @interface GameViewController : UIViewController<GamePrizeDelegate>
Suppose I have an interface IFoo with implementation classes VideoFoo , AudioFoo , and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.