Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass multiple generic arguments

i was wondering if there's a way to build a class which can accept multiple generic arguments that aren't known at compile time

   class Something<T,V,U> 

this example shows a class which would expect to receive 3 generic arguments at run time. i'm looking for a way to specify a class which would except multiple arguments of a varied amount

something along the line of

    class Something<T[]> 

which i could later expose using reflection

  Type [] types = GetType().GetGenericArguments(); 
like image 307
eran otzap Avatar asked Jul 29 '11 02:07

eran otzap


People also ask

How do you pass arguments to a generic type?

In addition, the x:TypeArguments attribute can be used to specify the generic type arguments to the constructor of a generic type. For more information, see Specifying a Generic Type Argument. Arguments can be passed to a non-default constructor using the x:Arguments attribute.

What is the type argument of genericlist<T>?

The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: In each of these instances of GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument.

How to use more than one type parameter in generics in Java?

You can also use more than one type parameter in generics in Java, you just need to pass specify another type parameter in the angle brackets separated by comma.

How to define a generic method in Java?

Thus, chose the type of the type parameter dynamically and pass the required object as a parameter. Similar to generic classes you can also define generic methods in Java. These methods use their own type parameters. Just like local variables, the scope of the type parameters of the methods lies within the method.


2 Answers

You can't specify an unknown number of generics. The closest you can get is to define all possible variations, or at least as many as you're willing to handle. I recommend using an abstract base class too. For example:

public abstract class Something { }
public class Something<T1> : Something { }
public class Something<T1, T2> : Something { }
public class Something<T1, T2, T3> : Something { }
public class Something<T1, T2, T3, T4> : Something { }
public class Something<T1, T2, T3, T4, T5> : Something { }
...

The abstract class is useful for when you need to reference the type but don't know the number of generic arguments.

Depending on your evil intentions you may end up writing a lot of redundant code using this solution in which case you should reconsider your use of generics.

like image 85
Keith Avatar answered Sep 22 '22 01:09

Keith


You can make some class - a kind of

public static class TypeHelper
{
    public static IEnumerable<Type> GetTypeCombination(this Type type)
    {
        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(T<,>))
            return type.GetGenericArguments().SelectMany(GetTypeCombination);

        return new Type[] { type };
    }
}

public class T<T1, T2>
{
    public static IEnumerable<Type> GetTypeCombination()
    {
        return typeof(T1).GetTypeCombination()
            .Concat(typeof(T2).GetTypeCombination());
    }
}

and to use that as

var list = T<int, T<string, int[]>>.GetTypeCombination().ToList();

to get (pass) dynamic list of types - not sure it's the best way

like image 22
DanNsk Avatar answered Sep 23 '22 01:09

DanNsk