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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:36:54+00:00 2026-05-23T15:36:54+00:00

I have the need for a method similar to this: // takes in a

  • 0

I have the need for a method similar to this:

// takes in a DefaultValue type as an Expression Function, with an argument
public void Register<T, TDefaultValueArgs>(Expression<Func<TDefaultValueArgs, T>> defaultValue)
{
    // stuff happens here.....
}

I would like to add two overloaded versions of the method. One where defautlValue is a simple instance of T, and another where defaultValue is an expression function without a paramater.

I’ve come up with this (untested) solution:

// takes in a DefaultValue type
public void Register<T>(T defaultValue)
{
    // The statement below would be better, but isn't valid: "A lambda expression with a statement body cannot be converted to an expression tree"
    // Expression<Func<object, T>> defaultValueFunc = (o) => { return defaultValue; };

    Expression<Func<DefaultValueContainer<T>, T>> defaultValueFunc = (m => m.GetDefaultValue(defaultValue));
    Register<T, DefaultValueContainer<T>>(defaultValueFunc);
}

// takes in a DefaultValue type as an Expression Function
public void Register<T>(Expression<Func<T>> defaultValue)
{
    // The statement below would be better, but isn't valid: "A lambda expression with a statement body cannot be converted to an expression tree"
    // Expression<Func<object, T>> defaultValueFunc = (o) => { return defaultValue.Compile().Invoke(); };

    Expression<Func<DefaultValueContainer<T>, T>> defaultValueFunc = (m => m.GetDefaultValue(defaultValue));
    Register<T, DefaultValueContainer<T>>(defaultValueFunc);
}

private class DefaultValueContainer<T>
{
    public DefaultValueContainer()
    { }

    public T GetDefaultValue(T defaultValue)
    {
        return defaultValue;
    }

    public T GetDefaultValue(Expression<Func<T>> defaultValue)
    {
        return defaultValue.Compile().Invoke();
    }
}

I’m then assuming, in my original Regsiter<T, TDefaultValueArgs> method, I could do something like this:

T resolvedDefaultValue = default(T);

if (typeof(TDefaultValueArgs).Equals(typeof(DefaultValueContainer<T>)))
{
    var defaultValueContainer = Activator.CreateInstance<TDefaultValueArgs>();
    resolvedDefaultValue = defaultValue.Compile().Invoke(defaultValueContainer);
}

All of this, including the use of DefaultValueContainer<T>, seems silly. There must be a better way?

Note: For reasons outside the scope of this example I am required to use an expression type, not just the delegate.

  • 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-05-23T15:36:54+00:00Added an answer on May 23, 2026 at 3:36 pm

    You can change your lambdas into expression lambdas without the need for your wrapper class.

    What you wanted in your code (but didn’t work):

    Expression<Func<object, T>> defaultValueFunc =
                                (o) => { return defaultValue; };
    

    What you should have:

    Expression<Func<object, T>> defaultValueFunc = 
                                (o) => defaultValue;
    

    Similarly, instead of:

    Expression<Func<object, T>> defaultValueFunc =
                                (o) => { return defaultValue.Compile().Invoke(); };
    

    You should have:

    Expression<Func<object, T>> defaultValueFunc =
                                (o) => defaultValue.Compile().Invoke();
    

    Making those changes should allow you to use your original Register<T> method.

    Also, I’m not sure if this is just an error in writing up the question, but you might want to align the generic type parameters in the original method, so that they are in the same order as the Func<> parameters. Just a style quibble, though.

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

Sidebar

Related Questions

I have this method that changes an larger image source on click. I need
I have a method that takes an array of queries, and I need to
I have method in a class that I need to make sure is only
I need to call a method I have defined as: -(IBAction)next:(id)sender{ ... } I
I have a JavaScript method that I need to run on one of my
I have use the TryUpdateModel method on the controllers but now I need to
I need to implement a call to a method that I have in my
I have a use case where I need to call a (non-static) method in
I have a COM interface and need to add an accessor method to it.
I have a service class that I need to execute a method on a

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.