Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:11:23+00:00 2026-06-15T05:11:23+00:00

Given the static class: public static class Converters { public static Func<Int64, string> Gold

  • 0

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() != 1)
{
    //throw new ConverterNotFoundException;
}                    
matchedValue = converter.Invoke(null, new object[]{matchedValue}) as string;

Edit:

I should have mentioned that I plan on adding other Funcs to my Converters class that may take different parameters.

Edit2:
From the replies so far, I have it working for the Gold Func below. I suppose my question now is, how do I make this work when I don’t know the parameters of the Func. For example, I may want to create another converter as so: Func<string, string>. The only thing I can be certain of is that there will only be one parameter (of differing types) and the return will always be string.

var converter = typeof(Converters).GetField("w", BindingFlags.Static | BindingFlags.Public);
if (converter == null)
{
    //throw new ConverterNotFoundException;
}
var f = converter.GetValue(null) as Func<Int64, string>;
matchedValue = f.Invoke(Convert.ToInt64(matchedValue));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:11:24+00:00Added an answer on June 15, 2026 at 5:11 am

    You need to specify BindingFlags to get static members:

    var converter = typeof(Converters).GetMember(regEx.Converter,
        BindingFlags.Static | BindingFlags.Public);
    

    You could also simplify this by using GetField if it will never be a property:

    FieldInfo converter = typeof(Converters).GetField(regEx.Converter,
        BindingFlags.Static | BindingFlags.Public);
    

    Edit:
    I’m not sure it’ll be much help as you’ll still need to know what sort of arguments to pass the Func<>, but this will let you invoke the Func<> without casting it:

    var matchedValue = converter.GetValue(null);
    matchedValue.GetType().GetMethod("Invoke")
        .Invoke(matchedValue, new object[] { Convert.ToInt64(0) });
    

    And to get the type of the argument:

    matchedValue.GetType().GetGenericArguments()[0];
    

    You should consider the possibility that it will be easier to switch on the field name and avoid reflection altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need Enumarable value from below class but give error public static Enumerable LoadDataByName(string
Given public class Original { public static DoStuff() { } } public class Derived
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following classes: <?php class test{ static public function statfunc() { echo this
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Why does trying to compile public class GenericsFail { public static void main(String[] args)
hy there! given is this class: public static class FooClass<TFoo> { public static TFoo
Given the following code: public static class Super { public static class Inner {
Given this code: public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final
Given a static class with an initializer method: public static class Foo { //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.