A
A
Alexander Kosheverov2014-01-03 13:20:14
C++ / C#
Alexander Kosheverov, 2014-01-03 13:20:14

Passing a List of Types to a Function

I have a function public void f(Type[] types)which, let's say, accepts types.
I need to register these types. To me it would look like this:

foreach(var type in types)
{
     Engine.RegisterType<type>();
}

Those. it is necessary to transfer each of the types to the template, but accordingly it is impossible, therefore I ask the question here: how to transfer a list of types to the function for their subsequent use in the template method?
The method is necessarily template (from third-party libraries). It is unlikely to be
used , since only 1 type is passed here, calling the function multiple times is also not an option.public void f<T>()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Andreev, 2014-01-04
@Iworb

To be honest, it's not very clear why you don't have information about the types that you need to register... and what's wrong with the f option? provide more complete information - there may be a better solution. however, here is the code that solves this problem:

var mi = Engine.GetType().GetMethod("RegisterType"); // хотя, этот тип уж точно известен), лучше typeof
foreach (var type in types)
    mi.MakeGenericMethod(new[] { type }).Invoke(null, null);

K
Kerman, 2014-01-04
@Kerman

No way. The template engine needs to get the concrete type at compile time. In this situation, the concrete type is only known at run time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question