【问题标题】:referencing a class from a different folder in c#在 C# 中引用来自不同文件夹的类
【发布时间】:2021-10-01 18:56:17
【问题描述】:

我有来自 2 个不同文件夹的 2 个程序,目前,我在 SectionA.cs 中有一个类(命名空间 SectionA,类名 Employee),我想重用 SectionB.cs(命名空间 SectionB)中的类中的一个方法,但是我不知道如何从 SectionB.cs 中引用一个类。我听说你必须使用using SectionA;,但它给了我这个错误

找不到类型或命名空间名称“SectionA”(您是否缺少 using 指令或程序集引用?)[SectionB]

下图为文件目录,请问如何引用SectionA.cs中的类在SectionB.cs中使用?提前谢谢你。

SectionA.cs

using System;
using System.IO;
using System.Collections.Generic;

namespace SectionA
{
    public delegate void generateDelegate(List<Employee> EmployeeList);
    public class Employee
    {
        public string Nric, FullName, Salutation, Designation, Department, MobileNo, HireType;
        public DateTime Start_Date;
        public double Salary, MonthlyPayout;

        public Employee(string Nric, string FullName, string Salutation, DateTime Start_Date, string Designation, string Department, string MobileNo, string HireType, double Salary)
        {
            this.Nric = Nric;
            this.FullName = FullName;
            this.Salutation = Salutation;
            this.Start_Date = Start_Date;
            this.Designation = Designation;
            this.Department = Department;
            this.MobileNo = MobileNo;
            this.Salary = Salary;
            this.MonthlyPayout = 0.0;
        }
        static public string CorpAdmin(List<Employee> EmployeeList)
        {
            string result = "";
            foreach (Employee employee in EmployeeList)
            {
                result += employee.FullName + "," + employee.Designation + "," + employee.Department + "\n";
            }
            return result;
        }
        static public string Procurement(List<Employee> EmployeeList)
        {
            string result = "";
            foreach (Employee employee in EmployeeList)
            {
                result += employee.Salutation + "," + employee.FullName + "," + employee.MobileNo + "," + employee.Designation + "," + employee.Department + "\n";
            }
            return result;
        }
        static public string IT(List<Employee> EmployeeList)
        {
            string result = "";
            foreach (Employee employee in EmployeeList)
            {
                result += employee.Nric + "," + employee.FullName + "," + employee.Start_Date + "," + employee.Department + "," + employee.MobileNo + "\n";
            }
            return result;
        }
        static public List<Employee> readHRMasterList()
        {
            List<Employee> EmployeeList = new List<Employee>();
            string direct = System.IO.Directory.GetCurrentDirectory();
            string path = "";
            string[] pathList = direct.Split("\\");
            for (int i = 0; i < pathList.Length - 1; i++) { path = path + pathList[i] + "\\"; }
            path += "HRMasterlist.txt";
            using (StreamReader sr = File.OpenText(path))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    string[] Emp = s.Split("|");
                    EmployeeList.Add(new Employee(Emp[0], Emp[1], Emp[2], Convert.ToDateTime(Emp[3]), Emp[4], Emp[5], Emp[6], Emp[7], Convert.ToDouble(Emp[8])));
                }
            }
            return EmployeeList;
        }
        static public void generateInfoForCorpAadmin(List<Employee> Employee_list)
        {
            string result = CorpAdmin(Employee_list);
            string path = @"CorporateAdmin.txt";
            string text = "";
            if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
            using (StreamReader sr = File.OpenText(path))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    if (s != "") { text = text + s + "\n"; }
                }
            }
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(text + result);
            }
        }
        static public void generateInfoForProcurement(List<Employee> Employee_list)
        {
            string result = Procurement(Employee_list);
            string path = @"Procurement.txt";
            string text = "";
            if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
            using (StreamReader sr = File.OpenText(path))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    if (s != "") { text = text + s + "\n"; }
                }
            }
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(text + result);
            }
        }
        static public void generateInfoForITDepartment(List<Employee> Employee_list)
        {
            string result = IT(Employee_list);
            string path = @"ITDepartment.txt";
            string text = "";
            if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
            using (StreamReader sr = File.OpenText(path))
            {
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    if (s != "") { text = text + s + "\n"; }
                }
            }
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(text + result);
            }
        }

        static void Main(string[] args)
        {
            List<Employee> EmployeeList = new List<Employee>();
            EmployeeList = readHRMasterList();
            generateDelegate del1 = generateInfoForCorpAadmin;
            generateDelegate del2 = generateInfoForProcurement;
            generateDelegate del3 = generateInfoForITDepartment;
            generateDelegate del = del1 + del2 + del3;
            del(EmployeeList);
        }
    }
}

SectionB.cs

using System;
using SectionA;
namespace SectionB
{
    class SectionB
    {
        static void Main(string[] args)
        {
        }
    }
}

【问题讨论】:

  • 请显示两个.cs文件的代码。
  • 您在使用 Visual Studio Code 吗?然后另请参阅stackoverflow.com/questions/42000798/…,了解如何添加参考。
  • @NoahStahl 我已经为两个 cs 文件添加了代码,希望在 SectionB.cs 中使用 readHRMasterList 方法
  • #include "../SectionA/SectionA.cs" 将添加对SectionA代码的引用。这样,当您为 #using SectionA 添加下面的行时,该命名空间将被识别。

标签: c# file class reference console-application


【解决方案1】:

您需要将SectionB 项目的引用添加到SectionA 项目,然后using 应该可以工作。

【讨论】:

  • 您好,感谢您的回答。请问,我应该如何将 SectionB 项目的引用添加到 SectionA 项目?
  • @3dsss 是的,我看到一些答案已经写了如何做到这一点。你可以在这里找到一个很好的详细解释:stackoverflow.com/questions/42000798/…
  • 谢谢,我看看!
  • 但是我有一个问题,如果我添加参考,我发送文件的人是否也必须添加参考,或者他不需要,因为我已经参考了它?
  • @3dsss 他不必这样做。添加引用时,它会更新 .csproj 文件的代码。这样您将发送的人也不需要这样做。
【解决方案2】:

您可以尝试打开项目的 SetrionB.csproj 文件并添加项目引用,如下所示:

<ItemGroup>
    <ProjectReference Include = "../SectionA.csproj" />
</ItemGroup>

之后你就可以在你的代码中使用它了

using ....
.....

【讨论】:

    【解决方案3】:

    如果你在 Visual Studio 中打开它,你可以右键单击 SectionB -> References -> Add Reference 并添加选择 SectionA 的引用。

    【讨论】:

    • 您好,感谢您的回答,我使用的是 Visual Studio Code 而不是 Visual Studio,因此我无法这样做
    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 2018-03-19
    • 1970-01-01
    • 2020-06-22
    • 2012-05-25
    • 2019-02-03
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多