I’m working with an api that requires a value of type Func. (Specifically, I’m trying to call ModelMetadataProviders.Current.GetMetadataForType().
How can I construct that value in F#?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When calling a method that takes any delegate of the
Funcyou shouldn’t need to explicitly create the delegate, because F# implicitly converts lambda expressions to delegate type (in member calls). I think that just calling the method with lambda function should work (if it doesn’t, could you share the error message?)Here is a simple example that demonstrates this:
In your case, I suppose something like this should work:
Note that you need explicit upcast (
expr :> obj), to make sure the lambda function returns the right type (obj). If you want to assign the lambda function to a local value usinglet, then it won’t work, because implicit conversion works only when it is passed as an argument directly. However, in that case, it makes the code a bit nicer.