【问题标题】:Option Strict disallows late binding using IEnumerableOption Strict 不允许使用 IEnumerable 进行后期绑定
【发布时间】:2018-07-25 07:08:44
【问题描述】:
Dim distinctJoints As IEnumerable
distinctJoints = From row In spotsTable.AsEnumerable()
                                        Group row By Key = row("FUNCJOINTCODE") Into Group
                                        Select Key, Group

_evaluatedJointsCount = (From row In spotsTable.AsEnumerable()
                        Group row By Key = row("FUNCJOINTCODE") Into Group
                        Select Key, Group).Count()

'Process each joint
For Each currentJoint In distinctJoints
    Dim currentJointKey As String = currentJoint.Key

对于上面的代码currentJoint.Key 在选项严格打开后显示后期绑定错误。 你能帮我解决这个问题吗?

【问题讨论】:

  • 不,它没有。您使用了错误的类型 - IEnumerable 与 IEnumerable 不同。在这种情况下,没有理由单独定义distinctJoints,只需编写Dim DistinctJoins = From ...,类型推断将使用正确的类型

标签: asp.net vb.net


【解决方案1】:

首先,让我祝贺您将代码移至Option Strict On!一开始可能会做一些工作,但从长远来看是有回报的,因为会在编译时而不是在运行时发现很多错误。

也就是说,让我们看看你的问题。这里:

Dim distinctJoints As IEnumerable

您将distinctJoints 声明为非泛型IEnumerable。非泛型 IEnumerable 在迭代时返回 Object 类型的项目。 Object 类型不包含 Key 方法。这就是您收到编译时错误的原因。

由于您的 LINQ 查询返回匿名类型的通用 IEnumerable,因此解决方案是改用 类型推断。在你的项目属性中激活Option Infer On(如果你还没有这样做的话)并让编译器推断出正确的数据类型:

' Dim distinctJoints As IEnumerable <-- remove this
Dim distinctJoints = From row In spotsTable.AsEnumerable()
                     Group row By Key = row("FUNCJOINTCODE") Into Group
                     Select Key, Group

【讨论】:

  • 谢谢,下面的代码还有一个问题,如果我试图从参数中删除 Ienumearable,那么它会在“spot1.row("XUNIT")" Private Sub EvaluateDistanceBetweenIrregularSpots(irregularWeldspots As IEnumerable) 上给出错误对于不规则焊接点中的每个点 1 如果点 1.row("XUNIT") 什么都没有,那么继续...
  • @PrasadK:我不确定我是否理解您评论中的问题。请创建一个minimal reproducible example 并为此发布一个新问题。
猜你喜欢
  • 2012-09-04
  • 2019-06-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多