Calling a static Func from a static class using reflection

Posted by ChrisO on Stack Overflow See other posts from Stack Overflow or by ChrisO
Published on 2012-11-29T23:03:12Z Indexed on 2012/11/29 23:03 UTC
Read the original article Hit count: 219

Filed under:
|
|

Given the static class:

public static class Converters
    {
        public static Func<Int64, string> Gold = c => String.Format("{0}g {1}s {2}c", c/10000, c/100%100, c%100);
    }

I am receiving the Func name from a database as a string (regEx.Converter). How can I invoke the Gold Func using reflection? Here is what I have so far:

var converter = typeof(Converters).GetMember(regEx.Converter);
                    if (converter.Count() != 0)
                    {
                        //throw new ConverterNotFoundException;
                    }

                    matchedValue = converter.Invoke(null, new object[]{matchedValue}) as string;

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET