How to pass an object to a type passed to a function?

This does not compile, but what I'm trying to do is just pass the 't' object that is passed to the function?

public void My_Func(Object input, Type t)
{
   (t)object ab = TypeDescriptor.GetConverter(t).ConvertFromString(input.ToString());
}
+5
source share
2 answers

You can do something like:

object ab = Convert.Changetype(input, t);

however, it looks like you want to use a abstrongly typed way, which you can only do with generics:

public void My_Func<T>(Object input)
{
   T ab = (T)Convert.ChangeType(input, typeof(T));
}
+13
source
public void My_Func(Object input, Type t)
{
    object test = new object();
    test = Convert.ChangeType(test, t);
    test = TypeDescriptor.GetConverter(t).ConvertFromString(input.ToString());
}
+1
source

All Articles