Lambda属性值选择器作为参数 [英] Lambda property value selector as parameter

查看:62
本文介绍了Lambda属性值选择器作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改一个方法,以便它具有一个额外的参数,该参数将带有一个lambda表达式,该表达式将用于内部对象以返回给定属性的值.原谅我可能不正确使用术语,因为这是我第一次尝试使用LINQ表达式!

I have a requirement to modify a method so that it has an extra parameter that will take a lambda expression that will be used on an internal object to return the value of the given property. Forgive my probable incorrect use of terminology as this is my first foray into LINQ expressions!

我尝试寻找答案,但是正如我提到的那样,我的术语似乎不正确,我可以找到的示例太复杂或无法处理我熟悉的集合函数(如.Where())的表达式

I have tried searching for an answer, but as I mentioned, my terminology seems to be off and the examples I can find are far too complex or deal with expressions for collection functions such as .Where(), which I am familiar with.

到目前为止,我有什么(精简版):

What I have so far (cut down version):

class MyClass
{
    private MyObject _myObject = new MyObject() { Name = "Test", Code = "T" };

    private string MyMethod(int testParameter, ??? selector)
    {
        //return _myObject.Name;
        //return _myObject.Code;
        return ???;
    }
}

我想这样称呼它:

string result = _myClassInstance.MyMethod(1, (x => x.Name));

或:

string result = _myClassInstance.MyMethod(1, (x => x.Code));

很明显,我缺少的部分是MyMethod中的selector参数,如何将其应用于局部变量以及在调用它时如何将所需的属性传递给方法.

Obviously the parts which I am missing is the selector parameter in MyMethod, how to apply it to the local variable and how to pass the required property into the method when I am invoking it.

任何帮助将不胜感激,VB.NET解决方案还可以获得额外的奖励积分,不幸的是最终实现需要在我们单独的VB项目中进行!

Any help would be appreciated, also extra bonus points for a VB.NET solutions as well as unfortunately the final implementation needs to be in our lone VB project!

推荐答案

private string MyMethod(int testParameter, Func<MyObject, string> selector)
{
    return selector(_myObject);
}

使用Func委托时,最后一个参数是返回类型,而第一个N-1是参数类型.在这种情况下,selector有一个MyObject参数,它返回一个string.

When using Func delegates, the last parameter is the return type and the first N-1 are the argument types. In this case, there is a single MyObject argument to selector and it returns a string.

您可以像这样调用它:

string name = _myClassInstance.MyMethod(1, x => x.Name);
string result = _myClassInstance.MyMethod(1, x => x.Code);

由于MyMethod的返回类型与selector委托的返回类型相匹配,因此可以使其通用:

Since the return type of MyMethod matches the return type of your selector delegate, you could make it generic:

private T MyMethod<T>(int testParameter, Func<MyObject, T> selector)
{
    MyObject obj = //
    return selector(obj);
}

我不知道VB.Net,但看起来像是这样:

I don't know VB.Net but it looks like it would be:

Public Function MyMethod(testParameter as Integer, selector as Func(Of MyObject, String))
    Return selector(_myObject)
End Function

,通用版本为:

Public Function MyMethod(Of T)(testParameter as Integer, selector Func(Of MyObject, T))
    Return selector(_myObject)
End Function

这篇关于Lambda属性值选择器作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆