Constraints on Type Parameter only elements which occur in an array of types

I have this code:

 Type[] typList = GetAllTypesFromAssembly(Assembly.LoadFile("aaaa.dddd.dll"));

And this statement, which matches if my desired type will be in that list:

typeList.Contains(typeof(T));

Now I want change the constraints on my type parameter to something like this:

public byte[] resultA<T>(string some, T myCustomGenericParameter) : where typeList.Contains(typeof(T));

How to do it correctly ?

Jon Skeet
people
quotationmark

You can't - because type constraints are designed to be verifiable by the compiler, whereas clearly typeList.Contains(typeof(T)) is only checkable at execution time.

The simplest approach is probably just to validate it as a precondition of the method. It won't give you any compile-time protection, but at least you can be confident for the rest of the method.

people

See more on this question at Stackoverflow