如何创建一个接受 lambda 表达式作为参数的方法?

发布于 2024-08-24 02:32:21 字数 522 浏览 4 评论 0原文

我使用以下代码将属性传递给 lambda 表达式。

namespace FuncTest
{
    class Test
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Name = "My Test";
            PrintPropValue(t => t.Name);

        }

        private static void PrintPropValue(Func<string> func)
        {
            Console.WriteLine(func.Invoke());
        }

    }
}

这不能编译。我只希望该函数能够获取属性并能够进行评估。

I am using the following code to pass a property to a lambda expression.

namespace FuncTest
{
    class Test
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Name = "My Test";
            PrintPropValue(t => t.Name);

        }

        private static void PrintPropValue(Func<string> func)
        {
            Console.WriteLine(func.Invoke());
        }

    }
}

This does not compile. I just want the function to be able to take property and be able to evaluate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Func 没有任何参数 - 但您的 lambda 表达式有。

目前尚不清楚您是否真的想要一个 Func - 在这种情况下,您需要传入一个 Test 实例当您调用委托时 - 或者您是否需要一个 Func 来捕获 Test 的特定实例。对于后者:

using System;

class Test
{
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.Name = "My Test";
        // Note: parameterless lambda expression
        PrintPropValue(() => t.Name);

        // Demonstration that the reference to t has been captured,
        // not just the name:

        Func<string> lambda = () => t.Name;
        PrintPropValue(lambda);
        t.Name = "Changed";
        PrintPropValue(lambda);
    }

    private static void PrintPropValue(Func<string> func)
    {
        Console.WriteLine(func.Invoke());
    }
}

A Func<string> doesn't have any parameters - but your lambda expression does.

It's not clear whether you really want a Func<Test, string> - in which case you'll need to pass in an instance of Test when you invoke the delegate - or whether you want a Func<string> which captures a particular instance of Test. For the latter:

using System;

class Test
{
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.Name = "My Test";
        // Note: parameterless lambda expression
        PrintPropValue(() => t.Name);

        // Demonstration that the reference to t has been captured,
        // not just the name:

        Func<string> lambda = () => t.Name;
        PrintPropValue(lambda);
        t.Name = "Changed";
        PrintPropValue(lambda);
    }

    private static void PrintPropValue(Func<string> func)
    {
        Console.WriteLine(func.Invoke());
    }
}
嗼ふ静 2024-08-31 02:32:21
class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.Name = "My Test";

        //Use a lambda with a free variable
        Func<Test, string> lambda = x => x.Name;
        PrintPropValue(t, lambda);

        //Close the lambda over its free variable.
        //It will refer to the t 
        //captured from the current scope from now on
        //Note: 'lambda' is unchanged, it still can be used 
        //with any 'Test' instance. We just create a (new) 
        //closure using the 'lambda'.
        Func<string> closure = () => lambda(t);
        PrintPropValue(closure);

        //This will still print 'My Test',
        //despite the fact that t in that scope 
        //refers to another variable.
        AnotherT(closure);

        t.Name = "All your " + t.Name + " captured and are belong to us.";
        //This will now print 'All your My Test captured and are belong to us.'
        AnotherT(closure);

    }

    private static void AnotherT(Func<string> closure)
    {
        Test t = new Test();
        t.Name = "My Another Test";

        PrintPropValue(closure);

    }

    private static void PrintPropValue<T>(T instance, Func<T, string> func)
    {
        Console.WriteLine(func(instance));
    }

    private static void PrintPropValue(Func<string> func)
    {
        Console.WriteLine(func());
    }

}
class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.Name = "My Test";

        //Use a lambda with a free variable
        Func<Test, string> lambda = x => x.Name;
        PrintPropValue(t, lambda);

        //Close the lambda over its free variable.
        //It will refer to the t 
        //captured from the current scope from now on
        //Note: 'lambda' is unchanged, it still can be used 
        //with any 'Test' instance. We just create a (new) 
        //closure using the 'lambda'.
        Func<string> closure = () => lambda(t);
        PrintPropValue(closure);

        //This will still print 'My Test',
        //despite the fact that t in that scope 
        //refers to another variable.
        AnotherT(closure);

        t.Name = "All your " + t.Name + " captured and are belong to us.";
        //This will now print 'All your My Test captured and are belong to us.'
        AnotherT(closure);

    }

    private static void AnotherT(Func<string> closure)
    {
        Test t = new Test();
        t.Name = "My Another Test";

        PrintPropValue(closure);

    }

    private static void PrintPropValue<T>(T instance, Func<T, string> func)
    {
        Console.WriteLine(func(instance));
    }

    private static void PrintPropValue(Func<string> func)
    {
        Console.WriteLine(func());
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文