代码之家  ›  专栏  ›  技术社区  ›  Amitabh

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

  •  1
  • Amitabh  · 技术社区  · 15 年前

    我使用以下代码将属性传递给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());
            }
    
        }
    }
    

    这不会编译。我只希望函数能够获取属性并能够计算。

    2 回复  |  直到 15 年前
        1
  •  8
  •   Jon Skeet    15 年前

    Func<string> 没有任何参数-但是lambda表达式有。

    不清楚你是否 真正地 想要一个 Func<Test, string> -在这种情况下,您需要传入 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());
        }
    }
    
        2
  •  2
  •   Andras Vass    15 年前
    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());
        }
    
    }
    
    推荐文章