【问题标题】:Why would the Visual C# 2008 Compiler think one explicitly declared and referenced type is another unreferenced one?为什么 Visual C# 2008 编译器会认为一种显式声明和引用的类型是另一种未引用的类型?
【发布时间】:2012-04-14 17:19:54
【问题描述】:

我有以下代码:

namespace FVProductions.Base
{
    public struct Color
    {
        public byte B, G, R, A;

        public Color(float r, float g, float b, float a)
        {
            R = (byte)(Math.Min(1.0f, Math.Max(0.0f, r)) * 255);
            G = (byte)(Math.Min(1.0f, Math.Max(0.0f, g)) * 255);
            B = (byte)(Math.Min(1.0f, Math.Max(0.0f, b)) * 255);
            A = (byte)(Math.Min(1.0f, Math.Max(0.0f, a)) * 255);
        }

        public Color(Vector3 rgb)
            :this(rgb.X,rgb.Y,rgb.Z,1)
        {
        }
    }
}

namespace FVProductions.Base.Graphics
{
    public class ShaderParameter<T>
    {
        private T Value;
        public T GetValue() { return Value; }
    }
}

namespace FVProductions.NewGame
{
    public class TerrainShader : Shader, IFullTextured, IStandardLit
    {
        private ShaderParameter<Vector3> epAmbient;

        public FVProductions.Base.Color AmbientColor
        {
            get { return new FVProductions.Base.Color(epAmbient.GetValue()); }
            set { epAmbient.SetValue(value.ToVector3()); }
        }
    }
}

类型 FVProductions.Base.Color 在引用的库中。 epAmbient.GetValue 返回一个 Vector3 并且 FVProductions.Base.Color 有一个带有单个 Vector3 参数的构造函数。该项目不引用 System.Drawing。但是,编译器正在生成以下错误:

错误 CS0012:“System.Drawing.Color”类型在未引用的程序集中定义。您必须添加对程序集“System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用。

位于 TerrainShader.AmbientColor get{} 行的 return 关键字处。为什么编译器会假定显式声明的类型是另一种类型?

【问题讨论】:

    标签: c# compiler-construction types declaration


    【解决方案1】:

    epAmbient.GetValue 很可能返回“System.Drawing.Color”,而不是您想象的 FVProductions.Base.Color。

    【讨论】:

    • epAmbient.GetValue 返回一个 Vector3。 FVProductions.Base.Color 有一个接受单个 Vector3 参数的构造函数。抱歉,我不清楚。
    • epAmbient 是什么类型的?考虑发布重现错误或至少看起来半完整的所有相关类和命名空间的示例......
    • epAmbient 是一个 ShaderParameter。我必须发布 很多 代码才能使其处于上下文中。让我看看我能做什么。
    • 尝试先将epAmbient.GetValue直接替换为new Vector3()。我敢打赌它会编译好的。
    • 不,用“new Vector3()”替换有同样的编译器错误。
    【解决方案2】:

    如果您的系统上安装了其他版本的 .NET,则您可能引用了错误的版本。确保 .config 文件中的所有引用都与 2.0 版本匹配,并确保没有将杂散的 .dll 复制到 bin 文件夹中。

    【讨论】:

    • 感谢您的提示。刚刚检查了系统引用,它们都是相同的运行时版本。不过,我没有我知道的 .config 文件。我正在使用 Xna 3.1。输出文件夹中也没有额外的 DLL。
    【解决方案3】:

    找到了。 FVProductions.Base.Color 是基库中的基类,并具有接受 System.Drawing.Color 的构造函数。有问题代码的项目没有引用 System.Drawing。使用 Color 类时,即使未使用 System.Drawing.Color 构造函数,也无法链接它。于是,我发现了两个解决方案:

    让上层库引用 System.Drawing 库,即使它不会被使用。

    从 FVProductions.Base.Color 中移除 System.Drawing.Color 构造函数。

    两者都将允许项目编译。

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 2017-01-23
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多