98
foreach (var filter in filters)
{
    var filterType = typeof(Filters);
    var method = filterType.GetMethod(filter, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static);
    if (method != null)
    {
        var parameters = method.GetParameters();
        Type paramType = parameters[0].ParameterType;
        value = (string)method.Invoke(null, new[] { value });
    }
}

How can I cast value to paramType? value is a string, paramType will probably just be a basic type like int, string, or maybe float. I'm cool with it throwing an exception if no conversion is possible.

saluce
  • 13,035
  • 3
  • 50
  • 67
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    possible duplicate of [How to lookup and invoke a .Net TypeConverter for a particular type?](http://stackoverflow.com/questions/956076/how-to-lookup-and-invoke-a-net-typeconverter-for-a-particular-type) – Ian Mercer Oct 24 '10 at 20:05
  • Possible duplicate of [How to lookup and invoke a .Net TypeConverter for a particular type?](http://stackoverflow.com/questions/956076/how-to-lookup-and-invoke-a-net-typeconverter-for-a-particular-type) – Michael Freidgeim Mar 16 '17 at 10:18
  • 7
    @MichaelFreidgeim's comment is a possible duplicate of the preceding one. – kmote Apr 09 '19 at 20:12
  • 2
    @kmote , the “possible duplicate” comment is usually automatically generated when someone vote to close the question as duplicate. Not sure why system inserted the same comment second time instead of increment vote of the first comment. May be the oldest comment was created manually, as it has low-case p in ‘possible’ – Michael Freidgeim Apr 10 '19 at 06:24
  • 1
    @kmote, it is a known issue https://meta.stackoverflow.com/questions/367526/merge-possible-duplicate-comments-and-votes – Michael Freidgeim Apr 11 '19 at 21:27

2 Answers2

107

The types you are using all implement IConvertible. As such you can use ChangeType.

 value = Convert.ChangeType(method.Invoke(null, new[] { value }), paramType);
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 20
    awesome ... in single line. var value = Convert.ChangeType(objectValue, objectType); – Rigin Jun 01 '16 at 15:08
17

You could go dynamic; for example:

using System;

namespace TypeCaster
{
    class Program
    {
        internal static void Main(string[] args)
        {
            Parent p = new Parent() { name = "I am the parent", type = "TypeCaster.ChildA" };
            dynamic a = Convert.ChangeType(new ChildA(p.name), Type.GetType(p.type));
            Console.WriteLine(a.Name);

            p.type = "TypeCaster.ChildB";
            dynamic b = Convert.ChangeType(new ChildB(p.name), Type.GetType(p.type));
            Console.WriteLine(b.Name);
        }
    }

    internal class Parent
    {
        internal string type { get; set; }
        internal string name { get; set; }

        internal Parent() { }
    }

    internal class ChildA : Parent
    {
        internal ChildA(string name)
        {
            base.name = name + " in A";
        }

        public string Name
        {
            get { return base.name; }
        }
    }

    internal class ChildB : Parent
    {
        internal ChildB(string name)
        {
            base.name = name + " in B";
        }

        public string Name
        {
            get { return base.name; }
        }
    }
}
Jakob Flygare
  • 189
  • 1
  • 2