views:

119

answers:

2

i have a class that has this property

public Expression<Action<Controller>> Action { get; set; }

how to set it's value for example:

var x = new MyClass{
Action = What_To_Write_here
}
+2  A: 

in same way as simple Action<Controller>

var x = new MyClass{
Action = controller => controller.DoSomething()
}
Yurec
so I must have an instance of this controller, actually all this class needs is to have the string name of the controller and the string name of the action
Omu
No, you don't. You will need instance in place where you will call compiled lambda.
Yurec
You don't need an instance of the controller. This is lambda syntax for defining an expression. This allows you to get at a limited syntax tree for C# code, which allows for some powerful metaprogramming. I'm guessing your class uses this expression to get at the controller and action name. It does this by parsing the syntax tree provided by the expression. So no actual controller instance is involved.
Sean Devlin
ya, i got it, my problem actually was that instead of Controller i need a template (T where T: Controller) but i don't want to make the class generic (cuz i'm binding it to a view), and since i can't make a generic property (like with a method) i don't know, i guess i'm just going to pass string
Omu
+1  A: 

To expand on Yurec's answer, lambdas provide syntactic support for both delegates and expressions. The compiler will usually be able to infer which is needed based on context. (As Yurec notes, this is the case for you.) So a lambda assigned to some delegate can be assigned to an expression wrapping the same delegate type.

Sean Devlin