Revision of type of passage as parameter

Without generics, I would like to pass a type as a parameter to a function without instantiating the type . The processing function must be able to limit the type, for example (for example, using System.Enum) there can be any type:

enum QuestionTypes { Great, Good, Huh, Dumb, Dumber }
// error - "QuestionTypes is a type but used like a variable"
static void Main(string[] args) { TypeHandler(QuestionTypes); }
static void TypeHandler(System.Enum enumType) { /* do stuff */ }

This is not the same as passing a type name, string, or instance. It seems reasonable that .NET should be able to pass in a type since the definition exists in compiled code. Is this work just for reflection, or can it be done without?

+3
source share
1 answer

So .... pass the parameter Type?

static void MyFunction(Type t)
{
  ...
}

MyFunction(typeof(QuestionTypes));
+7
source

Source: https://habr.com/ru/post/1795628/


All Articles