【问题标题】:Anonymous Type Name Collision匿名类型名称冲突
【发布时间】:2014-05-14 13:42:20
【问题描述】:

返回匿名类型的 linq 查询在执行时会引发以下错误。

The type '<>f__AnonymousType9<Name,Value>' exists in both    
'Customer.CustomerWeb.Presentation.dll' and 'Customer.CustomerContext.dll'

使用JetBrains dotPeek 我发现有 2 个编译器生成的类发生冲突。

Customer.CustomerContext.dll

 internal sealed class <>f__AnonymousType9<<PayrollSiteID>j__TPar, <IsActive>j__TPar>

Customer.CustomerWeb.Presentation.dll

 internal sealed class <>f__AnonymousType9<<Name>j__TPar, <Value>j__TPar>

两个生成的类都在根namespace 中。有什么方法可以将Anonymous Type classes 定向到每个程序集上的特定命名空间?简单的解决方法是向其中一个匿名查询添加第三个变量,但这更像是一种 hack。

【问题讨论】:

  • 您没有显示查询,但另一个简单的解决方法肯定是创建一个命名的非匿名类型。
  • 这是编译时错误吗?
  • @Damien_The_Unbeliever 违背了匿名类型的目的
  • 加载程序集的方式有什么不寻常之处吗?
  • 由于两个类都标记为内部,它们的名称不应该发生冲突 - 也许您的一个程序集使用 InternalsVisibleTo 属性向另一个显示其内部......

标签: c# linq anonymous-types


【解决方案1】:

我认为您想要类似以下的内容,您可以在命名空间中添加 using 语句,以便在名称解析中优先考虑:

using Customer.CustomerContext;
namespace yourNameSpace
{
    using Customer.CustomerWeb.Presentation; //Where f__AnonymousType9<Name,Value> exists
}

【讨论】:

    【解决方案2】:

    来自cmets

    它会导致 StackOverFlow。为了得到The type '<>f__AnonymousType9<Name,Value>' exists in both
    'Customer.CustomerWeb.Presentation.dll' and 'Customer.CustomerContext.dll'
    ,我必须在 碰撞。检查变量是当我收到该消息时

    命名空间冲突不是您的问题,它会导致调试器在尝试评估变量时崩溃,但我不知道它如何可能导致 StackOverflow。您需要 a) 更仔细地查看 linq 以找出意外递归的位置或 b) 发布堆栈跟踪的一部分,以便我们帮助您追踪它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-05
      • 2020-06-18
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2023-03-11
      • 2020-11-26
      相关资源
      最近更新 更多