1

I don't think this is possible in C# but is there any way to iterate a list of generic arguments? Something like this:

public class Test<T, T1, T2, T3, T4>
{
    public void DoIt()
    {
        foreach (var TItem in [T, T1, T2, T3, T4])
        {
            var foo = SomeService.Get<TItem>();

            /* or */

            var bar = new List<TItem>();
        }
    }
}

Pretty sure the answer is no due to CLR limitations but wanted to see if there's any way to accomplish this. I know you can do:

this.GetType().GetGenericArguments()

...but that yields a list of runtime Type objects which is not what I'm looking for.

Craig Koster
  • 496
  • 5
  • 15
  • Why? What are you trying to actually do? – SimpleVar Dec 09 '16 at 23:23
  • http://stackoverflow.com/questions/41029481/c-sharp-fluent-api-with-dynamic-func-construction – Craig Koster Dec 09 '16 at 23:26
  • Trying to create a re-useable method to materialize query results into strongly typed objects where the number of specified generic types is variable. – Craig Koster Dec 09 '16 at 23:26
  • No, you can't loop over generic parameters. There's not really any point to doing so since generics always have a fixed number of parameters (at best it would save you a small amount of typing). The body of that loop could probably be refactored into a generic method that can be called multiple times with different arguments, so you'd only have one line of code per generic parameter. – Kyle Dec 09 '16 at 23:46
  • Yes - this is to save typing but not just for one method. I have classes in a fluent API that are , , , , etc all the way up to <..., T16> and I want to create a common implementation of some code that will be able to handle all these different generic class argument lists in the same way instead of having to get extremely specific and verbose in every class. – Craig Koster Dec 10 '16 at 02:38

2 Answers2

1

I'm inclined to agree with @Kyle 's comment here (that this isn't all that valuable since it'll mostly just save typing).

With that said, this may not be exactly what you're looking for, but for what it's worth this code sample may be interesting:

private static void DisplayGenericTypes<T, Q, R, S>()
    {
        // The output of DisplayGenericTypes<int, string, double, float>(); is int, string, double, float
        foreach (Type t in new[] { typeof(T), typeof(Q), typeof(R), typeof(S)})
        {
            Console.WriteLine(t.Name);
        }

        Type[] generics = MethodInfo.GetCurrentMethod().GetGenericArguments();

        // Output is T, Q, R, S
        foreach (Type t in generics)
        {
            Console.WriteLine(t.Name);
        }
    }
1
using System;
using System.Reflection;

public class Program
{
    public static void Main()
    {               
        var instance = new Test<string, string, string, string, string>();
        instance.DoIt();
    }       
}

public class Test<T, T1, T2, T3, T4>
{       
    public void DoItAgain<T5>()
    {
        Console.WriteLine(typeof(T5));
    }

    public void DoIt()
    {           
        Type[] genericTypes = this.GetType().GetGenericArguments();
        foreach (var TItem in genericTypes)
        {               
            Console.WriteLine(TItem);   

            //Instead of 
            // var foo = SomeService.Get<TItem>();

            //Call generic method like this
            MethodInfo method = this.GetType().GetMethod("DoItAgain");
            MethodInfo generic = method.MakeGenericMethod(TItem);
            generic.Invoke(this, null);                             
        }                       
    }
}

Not sure what you are trying to accomplish. But this works.

whats-the-difference-between-system-type-and-system-runtimetype-in-c

how-do-i-use-reflection-to-call-a-generic-method

Community
  • 1
  • 1
Sumanth
  • 224
  • 2
  • 3