Link to home
Start Free TrialLog in
Avatar of David DB
David DB

asked on

Set type at runtime

How can the type be se in runtime in C#?

      public object Test(int whatType)
      {
         dynamic ret;
         if whatType = 1
         {
            T = blabla;
         }
         else
         {
            T = somethingelse;
         }

         ret = GetData<T>()
         return ret;
      }

Open in new window


Here I want to set T to a class to use it in GetData. This must be decided in runtime.
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

public object Test(int whatType)
        {
            dynamic ret;
            if (whatType == 1)
            {
                ret = GetData<int>();
            }
            else
            {
                ret = GetData<byte>();
            }
         return ret;
        }

Outside of doing what Lukasz Zielinski shows, the only other way to accomplish this would be through reflection:


using System;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            object x = Test(2);
 
            Console.WriteLine(x);
 
            Console.ReadKey();
        }
 
        public static object Test(int whatType)
        {
            dynamic ret;
            Func<object> func;
            Type t;
 
            if (whatType == 1)
            {
                t = typeof(int);
            }
            else
            {
                t = typeof(string);
            }
 
            func = GetGenericMethod(t); // Use reflection to create a delegate that represents the generic version of your method.
            ret = func();   // Invoke delegate
 
            return ret;
        }
 
        private static dynamic GetData<T>()
        {
            // Do something with T
 
            if (typeof(T) == typeof(string))
            {
                return "Hello World!";
            }
            else if (typeof(T) == typeof(int))
            {
                return 99;
            }
            else
            {
                throw new ArgumentException("Invalid type argument.");
            }
        }
 
        private static Func<object> GetGenericMethod(Type t)
        {
            Type program = typeof(Program);
            System.Reflection.MethodInfo methodInfo = program.GetMethod(nameof(GetData), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); // Change binding flags according to how your method is devined
            System.Reflection.MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(t);
            Func<object> func = (Func<object>)genericMethodInfo.CreateDelegate(typeof(Func<object>));
 
            return func;
        }
    }
}


@Kaufmed: :P Why would you ever want to do that nowadays though? :)

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.