简体   繁体   English

为什么LinqPad创建字段而不是属性?

[英]Why does LinqPad create Fields instead of Properties?

I recently took on a project of creating a tool for LinqPad that would Dump query results into CSV format in order to use the tool on massive databases for quick results. 我最近进行了一个为LinqPad创建工具的项目,该工具会将查询结果转储为CSV格式,以便在大型数据库上使用该工具以获得快速结果。 One thing I wanted out of the tool is for it to be able to work in Visual Studio, and LinqPad. 我希望该工具提供的一件事是使其能够在Visual Studio和LinqPad中工作。 Thus, if I was using LinqtoSQL in VS2010, or LinqPad, I could dump results quickly to a csv file, and then open it up into Excel to view the results. 因此,如果我在VS2010或LinqPad中使用LinqtoSQL,则可以将结果快速转储到csv文件中,然后将其打开到Excel中以查看结果。

The biggest hiccup in the project came from how LinqPad builds their DataContexts vs. how Visual Studio builds their DataContexts. 该项目最大的麻烦来自LinqPad如何构建其DataContext和Visual Studio如何构建其DataContext。 The best information I could find on how LinqPad does it comes from here . 我可以找到有关LinqPad如何做的最佳信息来自此处 Basically what I found from my project, was that VS2010 creates properties for their DataContexts, but LinqPad creates Fields. 从项目中我发现,基本上是VS2010为它们的DataContext创建属性,而LinqPad创建了Fields。 Thus when using reflection: 因此,在使用反射时:

LinqPad: LinqPad:

dataContextType.GetProperties() //returns 0
dataContextType.GetFields() //returns the Fields from LinqPad created DataContext

VS 2010 LinqToSQL: VS 2010 LinqToSQL:

dataContextType.GetProperties() //returns the Properties from VS created DataContext
dataContextType.GetFields() //returns 0

So why does LinqPad use Fields instead of Properties in their DataContexts? 那么,为什么LinqPad在其DataContext中使用字段而不是属性? Wouldn't it have been more feasible to copy the Visual Studio LinqToSQL pattern? 复制Visual Studio LinqToSQL模式不是更可行吗?

Update 更新资料

Based on a comment I decided to ask the same question within the LinqPad forum as well. 基于评论,我决定在LinqPad论坛中也问同样的问题。

This is a good question. 这是一个很好的问题。 The main reason for LINQPad using fields to map columns is for performance when building the typed DataContext that backs database-connected queries. LINQPad使用字段映射列的主要原因是在构建支持数据库连接查询的类型化DataContext时的性能。

We're not talking about the speed of executing the properties themselves (there's actually very little overhead in executing simple accessors and the JIT may even inline them.) The overhead is when building the typed DataContext via Reflection.Emit. 我们并不是在谈论执行属性本身的速度(实际上,执行简单访问器的开销很小,而JIT甚至可以内联它们。)开销是通过Reflection.Emit构建类型化DataContext时的开销。 A field is simply that: one item of metadata, whereas a property requires emitting a field definition, a property definition, two methods for the accessors (each with IL to get/set the underlying field). 一个字段就是这样:一个元数据项,而一个属性则需要发出一个字段定义,一个属性定义,两种访问器方法(每个方法都使用IL来获取/设置基础字段)。 Because users may point LINQPad to databases with upwards of 1000 tables and functions, this can add up in terms of the time taken to build the assembly - as well as its size (increasing HDD activity and working set). 因为用户可能将LINQPad指向具有多达1000个表和函数的数据库,所以这可能会增加构建程序集所花费的时间及其大小(增加HDD活动和工作集)。

You have raised an interesting issue in the lack of unification between PropertyInfo and FieldInfo in the reflection object model. 您已经提出了一个有趣的问题,即反射对象模型中PropertyInfo和FieldInfo之间缺乏统一性。 It would be nice if there was an interface that unified fields and (non-indexed) properties. 如果有一个接口可以统一字段和(非索引)属性,那就太好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Linq-2-Sql更新:编辑私有字段不会触发更新,编辑公共属性会更新。 为什么? - Updating with Linq-2-Sql: Editing private fields doens't trigger update, editing public properties does. Why? 为什么不能创建具有多个字段的关联? - Why can't I create an association with several fields? 为什么我在SQL Server中看到的字段的默认值没有反映在LINQ中 - Why the default that i see in SQL Server for the fields does no reflected in LINQ 使用XML数据库字段的Linq-to-SQL - 为什么这样做? - Linq-to-SQL With XML Database Fields — Why does this work? Linqpad 协会 - Linqpad associations 为什么LINQPAD假定此变量是System.Int32,我如何改变主意? - Why is LINQPAD assuming this variable is a System.Int32 and how can I change its mind? 为什么在添加主键后,Linq / Linqpad不能识别表的主键? - Why is Linq/Linqpad not recognising primary key of table after a primary key was added to it? LINQ to SQL的奥秘:为什么在某些情况下查询包含所有字段,而在其他情况下却不包括? - LINQ to SQL mystery: Why does query include all fields in some cases but not others? 为什么linq-2-sql会创建额外的不必要对象? - Why does linq-2-sql create extra unnecessary objects? 在Linq to SQL中,为什么分配相关实体会创建ChangeSet插入? - In Linq to SQL, why does assigning a related entity create a ChangeSet insert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM