Reflection.Emit添加的属性不可见 [英] Reflection.Emit added properties not visible

查看:97
本文介绍了Reflection.Emit添加的属性不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL中执行Pivot后,我得到一个数据源,其中包含一些已知列和可变数量的未知(在设计时)列。



I使用了经典的DataSet - DataTable解决方案,但在处理数据(60,000行和95列)时开始遇到OutOfMemory异常。



然后我意识到我需要一个更轻量级的解决方案所以使用Reflection.Emit在运行时构造我的类型。这完美地给了我比DataTables高10倍的性能,但是,处理代码非常复杂,所以为了简单起见,我改变了代码以生成一个类型,该类型继承自定义的基类型,其中基类型包含已知列和我使用Reflection.Emit来定义一个扩展基类的新类型,并在运行时添加额外的列。



这很好地简化了代码,我可以构建并完全构建正确填充这些对象。



问题是当我将它们绑定到DataGrid时,只有基本类型的列可见。如果我在调试器中添加对象的引用,我也只能看到基类的属性(Grid.DataSource(0)),但是如果我检查类型,我可以看到所有的属性都存在 - Grid.DataSource( 0).GetType.GetProperties()。



任何人都知道为什么DataGrid和其他数据可视化器拒绝找到这些添加的属性?

After performing a Pivot in SQL I end up with a datasource with a few known columns and a variable amount of unknown (at design time) columns.

I used the classic DataSet - DataTable solution, but started hitting OutOfMemory exceptions while processing the data (60,000 rows and 95 columns).

I then realized I needed a lighter weight solution and so used Reflection.Emit to construct my types at runtime. This worked perfectly giving me a 10x performance increase over the DataTables, however, the processing code was quite complex, so for simplicity sake I changed the code to generate a type that inherits from a defined base type where the base type contains the known columns and I used Reflection.Emit to define a new type extending the base class and adding the extra columns at run time.

This simplified the code nicely and I can construct and fully populate these objects correctly.

The problem is that when I bind them to a DataGrid, only the base type's columns are visible. If I add a reference to the object in the debugger I can also only see the properties of the base class (Grid.DataSource(0)), however if I check the type I can see all the properties are there - Grid.DataSource(0).GetType.GetProperties().

Anyone know why the DataGrid and other data visualizers refuse to find these added properties?

推荐答案

你必须给approrpate标志发射字段 - 例如 http://msdn.microsoft.com/en- us / library / system.reflection.fieldattributes%28v = vs.110%29.aspx [ ^ ]。
You have to give the approrpate flags to emit fields - e.g. http://msdn.microsoft.com/en-us/library/system.reflection.fieldattributes%28v=vs.110%29.aspx[^].


这篇关于Reflection.Emit添加的属性不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆