What causes “extension methods cannot be dynamically dispatched” here?

So, can somebody please help me understand why leveraging the same overload inside of those other methods is failing with that error? Precisely because you’re using a dynamic value (param) as one of the arguments. That means it will use dynamic dispatch… but dynamic dispatch isn’t supported for extension methods. The solution is simple though: … Read more

How to extend MVC3 Label and LabelFor HTML helpers?

You can easily extend the label by creating your own LabelFor: Something like this should do what you need public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) { return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = … Read more

Why is it impossible to declare extension methods in a generic static class?

Generally speaking, since you do not specify the class when you use an extension method, the compiler would have no way to know which is the class where the extension method is defined: static class GenStatic<T> { static void ExtMeth(this Class c) {/*…*/} } Class c = new Class(); c.ExtMeth(); // Equivalent to GenStatic<T>.ExtMeth(c); what … Read more

F# extension methods in C#

[<System.Runtime.CompilerServices.Extension>] module Methods = [<System.Runtime.CompilerServices.Extension>] let Exists(opt : string option) = match opt with | Some _ -> true | None -> false This method could be used in C# only by adding the namespace (using using) to the file where it will be used. if (p2.Description.Exists()) { …} Here is a link to the … Read more