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 7799393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:17:38+00:00 2026-06-02T00:17:38+00:00

is there any way to set Func<> type arguments dynamically, so i don’t have

  • 0

is there any way to set Func<> type arguments dynamically, so i don’t have to use endless if statements?

Something like:

Type t = Type.GetType("System.Decimal");
Func<t> foo = new Func<t>(some_function);

Instead of:

Func<Decimal> foo = new Func<Decimal>(some_function);

UPDATE:

Here’s a snippet from my code:

Type t = typeof(StavkaDokumenta).GetProperty(pd.Polje).PropertyType;
ParameterExpression pe = Expression.Parameter(typeof(StavkaDokumenta), "stavka");
Expression expr = Expressions.ResolveCompleteExpression(pe, pd.Expression);
Expression final = Expression.Convert(expr, t);
if (t == typeof(decimal))
{
    var lambda = Expression.Lambda<Func<StavkaDokumenta, decimal>>(final, pe);
    o = lambda.Compile().Invoke(stavka);
}
if (t == typeof(decimal?))
{
    var lambda = Expression.Lambda<Func<StavkaDokumenta, decimal?>>(final, pe);
    o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(int))
{
    var lambda = Expression.Lambda<Func<StavkaDokumenta, int>>(final, pe);
    o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(int?))
{
    var lambda = Expression.Lambda<Func<StavkaDokumenta, int?>>(final, pe);
    o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(string))
{
    var lambda = Expression.Lambda<Func<string>>(final, null);
    o = lambda.Compile().Invoke();
}

pd.Polje is string – name of a property inside “StavkaDokumenta” class.
pd.Expression is string expression that must evaluate to type of Polje.
stavka is an instance of StavkaDokumenta.

  • 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-02T00:17:40+00:00Added an answer on June 2, 2026 at 12:17 am

    Now that you showed what you really want, the answer is much more simple:
    As you are apparently only interested in the return value of that expression, you could change your code to this:

    Type t = typeof(StavkaDokumenta).GetProperty(pd.Polje).PropertyType;
    ParameterExpression pe = Expression.Parameter(typeof(StavkaDokumenta), "stavka");
    Expression expr = Expressions.ResolveCompleteExpression(pe, pd.Expression);
    Expression final = Expression.Convert(expr, t);
    
    if (t == typeof(string))
    {
        var lambda = Expression.Lambda<Func<string>>(final, null);
        o = lambda.Compile().Invoke();
    }
    else
    {
        var lambda = Expression.Lambda(final, pe);
        o = lambda.Compile().DynamicInvoke(stavka);
    }
    

    Old answer:

    You can use generics and implicit conversion to Func<T> to achieve this:

    Func<T> GetFunc<T>(Func<T> func)
    {
        return func;
    }
    

    Call it with a method group like so:

    var foo = GetFunc(SomeMethod);
    

    This assumes that SomeMethod looks like this:

    decimal SomeMethod()
    {
        // ...
    }
    

    foo will be of type Func<decimal>. If the return type of SomeMethod would be a string, the type of foo would be Func<string>.


    What happens in this code is the following:

    The parameter that is passed to GetFunc is a so-called “method group” and not a variable of type Func<T>. However, there exists an implicit conversion from a method group to a variable of Func<T>:

    Func<decimal> func = SomeMethod; // an implicit conversion happens here
    

    That implicit conversion is exactly what is happening here: Before GetFunc is even called, the method group SomeMethod is converted the a variable of type Func<T>. The concrete type used for T is additionally inferred by the compiler based on the return type of the method SomeMethod().
    Our goal was to create an instance of Func<T> based on our method group. And because that already happens in the conversion of the parameter before the method is even called, we simply return that created instance from the method.

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

Sidebar

Related Questions

is there any way to set the friction of a physics body dynamically in
Is there any way to set an array recursively? something along these lines: $array
Is there any way to set a CSS class on an input item in
Is there any way to set a cookie that is not readable on subdomains?
Is there any way to set the HTTP header fields of a HTML form
Is there any way to set a background view on a UITableViewController? I try
Is there any way to set the directory where you want to start a
Is there any way to set/update values in the settings.bundle from within your app.
Is there any way to set the default value for a column as an
IS there any way to set the maximumtime a cookie can be inactive in

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.