Is it somehow possible to send a function to another function for it to run.
For instance…I want to have an attribute on a field where I can specify a method that will be sent to another method where the method passed in will be run.
Not sure if that makes any sense but here’s a small example.
[ValidateIf(x=>x.test())]
public string test { get; set; }
Update:
Basically I want to use DataAnnotations for validation but sometimes depending on the system settings a field may not be mandatory…Do you have any suggestions on the direction I should head?
Unfortunately you can’t use delegates as properties for attributes. You’d have to specify the method name and then create the delegate using reflection at execution time – which obviously isn’t ideal in terms of finding typos at compile time.
It might be possible in IL (specifying a
MethodInfousing an equivalent totypeof) but C# doesn’t expose any way of doing this 🙁 Maybe one day we’ll get theinfoofoperator… quite how it would be exposed, I’m not sure. (It would be good to keep the delegate type information in the attribute.)