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.
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 ofTestwhen you invoke the delegate – or whether you want aFunc<string>which captures a particular instance of Test. For the latter: