在初始化组件之前初始化表单类级别组件集合的干净优雅的解决方案?
我正在 vs2010 中将旧的 Vb6 解决方案转换为 .net 2.0。我已经在 C# 领域工作了大约 3 年,在 .net 领域工作了 5 年。我不记得在 C#
中遇到过这个问题,但如果我想初始化一个 readonly
DerivedControlFoo
的集合 除了创建一个子程序在其他地方完成这一切之外,是否有一种干净的方法可以做到这一点?为了可读性和简单性,我希望能够在声明的类级别上执行此操作。
示例:
Private _formattedFormulaText As IEnumerable(Of Label) = New List(Of Label) From { _
FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
lblOrangeFormula, lblRedFormula, FormulaLabels7, lblFormulaTotal}
以简单的方式进行操作,会导致集合填充 {nothing,..,nothing}
I am converting an old Vb6 solution to .net 2.0 in vs2010. I've been working in C# for about 3 years now and .net for 5. I don't recall having this problem in C#
, but if I want initialize a readonly
collection of DerivedControlFoo
Is there a clean way to do it besides creating a sub to do it all off somewhere else? I'd love to be able to do it at the class level at the declaration for readability and simplicity.
Example:
Private _formattedFormulaText As IEnumerable(Of Label) = New List(Of Label) From { _
FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
lblOrangeFormula, lblRedFormula, FormulaLabels7, lblFormulaTotal}
Doing it the straightforward easy way, results in the collection being filled with {nothing,..,nothing}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是线程安全的,但非常适合我的目的:
使用中的解决方案:
缺点 - 所有涉及变量的代码都必须添加访问器
.value
。我的代码有 5 个惰性集合,每个集合有 1-2 个接触点,通常位于同一函数中。缺点说明:
变得
不符合 .net 4 中使用的调试器显示的良好实践,但使用反射器可以轻松添加
请注意,可以使用属性来消除接触点中 .value 的需要:
支持类:
设计基于 MSDN 的文章 C# 中的延迟计算
另外在 Reflector 的帮助下
This is not thread safe, but works very well for my purposes:
Solution in use:
drawbacks - all code touching the variable has to add an accessor
.value
. My code has 5 of these lazy collections and 1-2 touchpoints per collection usually in the same function.Drawback illustration:
becomes
Not compliant with good practices for debugger display used in .net 4, but would be easy to add in using reflector
Please note a property could have been used to eliminate the need to .value in the touchpoints:
supporting class:
design based on MSDN's article Lazy Computation in C#
Also with assistance from Reflector against