Is there a way to create a class with a list of dynamic types?

58 Views Asked by At

I'm trying to create a controller class for my database, but some tables only have one primary key, and some have multiple. And the data type of the primary key differs between some tables.

I would like to create a generic class with a list of dynamic types for the different types of keys

public class GenericClass<Model, [Types]<Type>> {
    public GenericClass() {
        foreach(Type t in Types) Console.WriteLine(t);
    }

    public Model Get(Types[0] Id1) {
        return new Model(Id1);
    }
}

Where Types is a list of types.

Is anything like this possible in C#?

1

There are 1 best solutions below

0
Hossein Sabziani On

What happens if you have a list of Type?

List<Type> Types = new List<Type>() { typeof(string), typeof(int) };
foreach (Type t in Types)
        Console.WriteLine(t);

result:

System.String
System.Int32