开发者

How can I pass various Expression Functions in overloaded methods?

开发者 https://www.devze.com 2023-03-17 01:41 出处:网络
I have the need for a method similar to this: // takes in a DefaultValue type as an Expression Function, with an argument

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) so开发者_JAVA百科lution:

// 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号