C# Assert at compile time Func argument is static

Is there any way at compile time to assert an argument Func is static?

This seems like something the compiler could easily check rather than relying on runtime failure.

I realise it can be done at runtime with [arg].Method.IsStatic

Cheers

Jon Skeet
people
quotationmark

This seems like something the compiler could easily check rather than relying on runtime failure.

Yes, it absolutely could - but only if there's something in the language to prompt it to do so. There isn't, in C# - it's a pretty odd requirement, IMO, and one which doesn't play nicely with lambda expressions.

With VS 2015 you could easily write a Roslyn CodeDiagnostic to spot this and throw up a warning or error. You'd probably want to decorate the parameter with an attribute for the diagnostic to spot, and then check all callers.

people

See more on this question at Stackoverflow