且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架/LINQ to SQL数据绑定是否使用反射?

更新时间:2023-02-05 11:34:45

支持LINQ等的Expression API是基于反射(MemberInfo)而不是组件模型(PropertyDescriptor等)的,因此没有ITypedList的巨大需求.而是从IQueryable<T>IEnumerable<T>IList<T>等中的T推断内容.

The Expression API that underpins LINQ etc is founded on reflection (MemberInfo), not the component-model (PropertyDescriptor etc) - so there is not a huge requirement for ITypedList. Instead, the contents are inferred from the T in IQueryable<T>, IEnumerable<T> and IList<T> etc.

您可能会得到的最接近的是IListSource,但这仍然只是适当的类型列表的浅层包装.

The closest you might get is IListSource, but that will still just be a shallow wrapper around a proper typed list.

如果运行时绑定(到PropertyDescriptor)的性能很关键,则可能需要查看

If performance of runtime binding (to PropertyDescriptor) is key, you might want to look at HyperDescriptor - which uses Reflection.Emit and TypeDescriptionProvider to wrap the component-model.

重新输入为什么"等;请注意,在使用LINQ-to-SQL和EF的几乎所有情况下,Expression(或创建和设置成员"部分)在被调用之前都将被编译成委托-因此在运行时不会有巨大的反映.成本.同样,使用LINQ-to-Objects,所有内容都已经通过C#编译器进行了编译.

Re the "why" etc; note that in almost all cases with LINQ-to-SQL and EF the Expression (or the "create and set members" part) is going to be compiled to a delegate before it is invoked - so at runtime there is no huge reflection cost. And likewise, with LINQ-to-Objects everything is already compiled (by the C# compiler).