Robust reflection using F# quotations
Reflection is useful but fragile. Consider this:
The problems with this kind of code are:
- The code doesn't work because there are several overloads of
- Even if there wouldn't be any overloads right now later versions of the library might add an overload causing a runtime crash
- Refactoring tools like
Rename methodsis broken with reflection.
This means we get a runtime crashes for something that is known compile-time. That seems suboptimal.
F# quotations it's possible to avoid all the problems above. We define some helper functions:
We use the functions like this:
<@ ... @> means that instead of executing the expression inside
F# generates an expression tree representing the expression.
<@ "".StartsWith "" @> generates an expression tree that looks like this:
Call (Some (Value ("")), StartsWith, [Value ("")]).
This expression tree matches what
getMethodInfo expects and it will return the correct method info.
This resolves all the problems listed above.