CS0433错误:类型’type’存在于’ProjectA’和’ProjectB’中。

huangapple go评论69阅读模式
英文:

CS0433 The type 'type' exists in 'ProjectA' and 'ProjectB'

问题

我有一个使用.NET Framework 4.7(C#)制作的解决方案,其中有两个项目在相同的命名空间中包含相同的部分类,这些项目被打包为一个nupkg

从现在开始,我们将遵循以下逻辑:

  • 项目A:包含部分类的一部分并引用项目B
  • 项目B:包含部分类的另一部分(自动生成的代码)
  • 项目C:此项目引用了nupkg,但如果我们在编译时尝试调用所提到的类的任何属性,会出现以下错误:
    CS0433 类型 'Config' 存在于 'ProjectA, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null' 和 'ProjectB, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null'

我已经尝试过:

  • 清理和重新构建。
  • 删除并再次添加引用。
  • 删除多余和无用的引用。
  • 从项目C卸载NuGet并重新安装。

以下是每个项目中冲突的代码片段:

项目A:部分类 Config

namespace WhatIsWrong
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Config
    {
    }
}

项目B:部分类 Config

namespace WhatIsWrong
{
    using System;
    using System.Collections.Generic;

    public partial class Config
    {
        public string parameter { get; set; }
        public string value { get; set; }
    }
}

项目C

public DbSet<WhatIsWrong.Config> Config { get; set; }

更新:

我已经考虑到部分类必须在同一个项目中,就像Jon和Damien在评论中指出的那样,但这个解决方案是由别人创建的,应该正常工作,我猜他的想法是为两个项目使用一个模型(这并没有错),但他还尝试在两个项目中使用部分类,最终导致了这个错误。

英文:

I have a solution made with .NET Framework 4.7 (c#) in which we have 2 projects that contain the same partial class within the same namespace, those projects are packaged on a nupkg.

From now on we will follow the following logic:

  • Project A: has a part of the partial class and reference to Project B
  • Project B: has another part of the partial class (code generated automatically)
  • Project C: this project has the nupkg referenced but if we try to call any property of the mentioned class when compiling, it gives the following error:
    CS0433 The type 'Config' exists in 'ProjectA, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null' and in 'ProjectB, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null'

I've already tried:

  • Cleaning and rebuilding.
  • Deleting and adding again the references.
  • Removing redundant and useless references.
  • Uninstalling the NuGet from Project C and installing it again.

Here are the conflicting code snippets in each project:

Project A: partial class Config

namespace WhatIsWrong
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Config
    {
    }
}

Project B: partial class Config

namespace WhatIsWrong
{
    using System;
    using System.Collections.Generic;

    public partial class Config
    {
        public string parameter { get; set; }
        public string value { get; set; }
    }
}

Project C

public DbSet<WhatIsWrong.Config> Config { get; set; }

Update:

I already thought that partial classes had to be on the same Project like Jon and Damien pointed on the comments, but this solution was created by someone else and was supposed to be working, I guess his idea was to have just one model for both projects (what is not wrong) but he also tried to play with partials in both projects what ended with that error.

答案1

得分: 4

> 项目A:有一个部分类的一部分,并引用了项目B
> 项目B:有另一个部分类的一部分(自动生成的代码)

这不是部分类的工作方式。你在两个项目中有两个类。

部分类的每个部分都需要在同一个项目中,因为它们一起编译为一个类型。

部分类允许将单个类型的代码分布在多个文件中,但不能分布在多个项目中。

英文:

> Project A: has a part of the partial class and reference to Project B
> Project B: has another part of the partial class (code generated automatically)

That's not how partial classes work. What you've got there is two classes in two projects.

Every part of a partial class needs to be in the same project, because they're compiled together to be one type.

Partial classes allow a single type's code to be distributed amongst multiple files - but not multiple projects.

huangapple
  • 本文由 发表于 2023年6月12日 22:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定