单独文件中的 C# 命名空间/类在查看时导致错误

标签 c# class namespaces using

我有两个项目“HOD”和“ Controller ”。 Controllers 是一个名为“Controllers”的类库。在 HOD 的代码文件中,我试图通过“使用 Controller ”来引用 Controller 命名空间。我在 HOD 项目中添加了一个对 Controllers 的引用,它确实显示在 VS2008 中的references 文件夹下。

当我构建项目时,我没有发现任何错误,但是当我查看页面时,我收到错误消息“找不到类型或命名空间名称‘ Controller ’(您是否缺少 using 指令或程序集引用?)” .

Controllers.cs(类库,Controllers 项目)

using System;
using System.Collections.Generic;
using System.Text;

namespace Controllers
{

    public class Controller {    }

}

index.aspx.cs(HOD 项目)
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controllers;


public partial class HOD : Page
{

    string sop() { return "sop"; }

    protected void Page_Load(object sender, EventArgs e)
    {
        blahmsg.Text = sop();
    }

}

该项目位于我们的网络服务器上,Controllers.dll 与 HOD.dll 位于同一个 bin 文件夹中。在此先感谢您的帮助!

编辑:.sln 内容
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "T:", "T:\", "{1D5F28B3-E6A0-4CF8-B7A3-43121225C73E}"
    ProjectSection(WebsiteProperties) = preProject
    TargetFramework = "2.0"
    Debug.AspNetCompiler.VirtualPath = "/T:"
    Debug.AspNetCompiler.PhysicalPath = "T:\"
        Debug.AspNetCompiler.TargetPath = "E:\DEV\webdev\webdev\PrecompiledWeb\T:\"
        Debug.AspNetCompiler.Updateable = "true"
        Debug.AspNetCompiler.ForceOverwrite = "true"
        Debug.AspNetCompiler.FixedNames = "false"
        Debug.AspNetCompiler.Debug = "True"
        Release.AspNetCompiler.VirtualPath = "/T:"
        Release.AspNetCompiler.PhysicalPath = "T:\"
        Release.AspNetCompiler.TargetPath = "E:\DEV\webdev\webdev\PrecompiledWeb\T:\"
        Release.AspNetCompiler.Updateable = "true"
        Release.AspNetCompiler.ForceOverwrite = "true"
        Release.AspNetCompiler.FixedNames = "false"
        Release.AspNetCompiler.Debug = "False"
        VWDPort = "53327"
    EndProjectSection
EndProject

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HOD", "Y:\HOD\HOD.csproj", "{5F523FB7-B902-4E6E-BAA9-C2EB37D69EC7}"
EndProject

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Controllers", "Y:\HOD\Controllers\Controllers.csproj", "{E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Release|Any CPU = Release|Any CPU
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {C8A12BBE-4483-4560-97A4-795F2E57D667}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {C8A12BBE-4483-4560-97A4-795F2E57D667}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {C8A12BBE-4483-4560-97A4-795F2E57D667}.Release|Any CPU.ActiveCfg = Debug|Any CPU
        {C8A12BBE-4483-4560-97A4-795F2E57D667}.Release|Any CPU.Build.0 = Debug|Any CPU
        {A81C381B-1E12-49B0-8BC0-8532EF9F216F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {A81C381B-1E12-49B0-8BC0-8532EF9F216F}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {A81C381B-1E12-49B0-8BC0-8532EF9F216F}.Release|Any CPU.ActiveCfg = Debug|Any CPU
        {A81C381B-1E12-49B0-8BC0-8532EF9F216F}.Release|Any CPU.Build.0 = Debug|Any CPU
        {BD46969E-9D1C-419A-A82C-388E35BF7C13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {BD46969E-9D1C-419A-A82C-388E35BF7C13}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {BD46969E-9D1C-419A-A82C-388E35BF7C13}.Release|Any CPU.ActiveCfg = Debug|Any CPU
        {BD46969E-9D1C-419A-A82C-388E35BF7C13}.Release|Any CPU.Build.0 = Debug|Any CPU
        {1D5F28B3-E6A0-4CF8-B7A3-43121225C73E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {1D5F28B3-E6A0-4CF8-B7A3-43121225C73E}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {1D5F28B3-E6A0-4CF8-B7A3-43121225C73E}.Release|Any CPU.ActiveCfg = Debug|Any CPU
        {1D5F28B3-E6A0-4CF8-B7A3-43121225C73E}.Release|Any CPU.Build.0 = Debug|Any CPU
        {5F523FB7-B902-4E6E-BAA9-C2EB37D69EC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {5F523FB7-B902-4E6E-BAA9-C2EB37D69EC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {5F523FB7-B902-4E6E-BAA9-C2EB37D69EC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {5F523FB7-B902-4E6E-BAA9-C2EB37D69EC7}.Release|Any CPU.Build.0 = Release|Any CPU
        {E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}.Release|Any CPU.Build.0 = Release|Any CPU
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal

编译器输出

------ 重建全部开始:项目: Controller ,配置:调试任何 CPU ------
C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe/noconfig/nowarn:1701,1702/errorreport:prompt/warn:4/define:DEBUG;TRACE/reference:"C:\Program Files ( x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll"/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions。 dll"/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll/reference:C :\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll "/debug+/debug:full/filealign:512/optimize-/out:obj\Debug\Controllers.dll/target:library Class1.cs Properties\AssemblyInfo.cs

编译完成 -- 0 个错误,0 个警告
Controller -> Y:\HOD2\Controllers\bin\Debug\Controllers.dll
------ 重建全部开始:项目:HOD,配置:调试任何 CPU ------
C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe/noconfig/nowarn:1701,1702/errorreport:prompt/warn:4/define:DEBUG;TRACE/reference:Y:\HOD2\Controllers\bin\Debug\Controllers.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Configuration.dll/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll"/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll"/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.EnterpriseServices.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll/reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Web.Extensions.Design.dll"/reference:"C:\Program Files (x8) 6)\Reference Assemblies\Microsoft\Framework\v3.5\System.Web.Extensions.dll"/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.Mobile.dll/reference: C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.Services.dll/reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll/reference:"C :\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll"/debug+/debug:full/optimize-/out:obj\Debug\HOD.dll/target:library index.aspx.cs index.aspx.designer.cs Properties\AssemblyInfo.cs

编译完成 -- 0 个错误,0 个警告
HOD -> Y:\HOD2\bin\HOD.dll
========== 全部重建:2 成功,0 失败,0 跳过 ==========

最佳答案

chelfers,感谢您在此处发布 sln 文件。根据它,您没有到位的项目引用。

您可以从 Visual Studio 添加它(删除引用、保存、添加引用),或者您可以添加以下行:

ProjectReferences = "{E6A16990-5FC6-4FE3-8DAB-CD2FEC8CF4FF}|Controllers.dll;"

正下方
ProjectSection(WebsiteProperties) = preProject
    TargetFramework = "2.0"

(为了安全起见,请先复制 .sln 文件)。

PS:移动到 3.5,这是二十一世纪:)

关于单独文件中的 C# 命名空间/类在查看时导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2375102/

相关文章:

c# - 打开自定义文件,args 始终为空

c# - 字符串和 Enumerable.Count

c++ - 如何将 C++ 代码中的枚举转换为 C

sql-server-2008 - 使用 T-SQL 和 XQuery 解析 TFS 2010 流程参数 XML

c++ - C++ 中的类是命名空间吗?

c# - WPF 容器 : equal width for elements but with spacing between them

c# - 如何在不使用 Task.Delay 或 await 的情况下编写异步方法

Java:使用actionlistener在另一个类中的对象上调用该类中的函数

javascript - 如何使用 ES6 类创建一个不从 Object.prototype 继承的类?

c++何时将方法放在类之外