Why does WebGrid use dynamic formatting?

I work with System.Web.Helpers.WebGrid in an ASP.NET MVC 3 Razor project, and it’s hard for me to understand why the format parameter for WebGridColumn is Func<dynamic, object> .

If I create a column like this ...

 grid.Column( format: x => string.Format("{0:d}", x.StartDate) ); 

... I do not get strong print in the StartDate property. If I try to get around this so ...

 grid.Column( format: (MyObjectType x) => string.Format("{0:d}", x.StartDate) ); 

... I am told at runtime that my lambda cannot be added to Func<dynamic, object> . Is there a way to use a non-dynamic lambda here? Even if it's just <object, object> ?

(I'm in .NET 4.0, and Func<in T, out TResult> should be contravariant on T, but I'm confused about how covariance and contravariance work with dynamic ones.)

+6
source share
1 answer

As for the type system, dynamic same as object .

They cannot use a strongly typed delegate because they do not have a strongly typed value for transmission.

Inside the WebGrid they get an object from the PropertyDescriptor and pass it on to your delegate.

Covariance does not help here; if Func<YourType, string> was converted to Func<object, string> , you could call it any other type and get an invalid listing.

+3
source

Source: https://habr.com/ru/post/887392/


All Articles