从 c# 中的不同文件夹引用 class
referencing a class from a different folder in c#
我有来自 2 个不同文件夹的 2 个程序,目前,我在 SectionA.cs(命名空间 SectionA,class 名称 Employee)中有一个 class,我想重用来自SectionB.cs(命名空间 SectionB)中的 class,但我不知道如何从 SectionB.cs 引用 class。我听说你必须使用 using SectionA;
但它给了我这个错误
The type or namespace name 'SectionA' could not be found (are you missing a using directive or an assembly reference?) [SectionB]
下图是文件目录,请问如何引用SectionA.cs中的class在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)
{
}
}
}
您需要将 SectionB
项目的引用添加到 SectionA
项目,然后 using
应该可以工作。
如果在Visual Studio中打开,可以右击SectionB -> References -> Add Reference 添加selectSectionA的引用
您可以尝试打开项目的 SectrionB.csproj 文件并添加项目引用,如下所示:
<ItemGroup>
<ProjectReference Include = "../SectionA.csproj" />
</ItemGroup>
之后您就可以在您的代码中使用它了
using ....
.....
我有来自 2 个不同文件夹的 2 个程序,目前,我在 SectionA.cs(命名空间 SectionA,class 名称 Employee)中有一个 class,我想重用来自SectionB.cs(命名空间 SectionB)中的 class,但我不知道如何从 SectionB.cs 引用 class。我听说你必须使用 using SectionA;
但它给了我这个错误
The type or namespace name 'SectionA' could not be found (are you missing a using directive or an assembly reference?) [SectionB]
下图是文件目录,请问如何引用SectionA.cs中的class在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)
{
}
}
}
您需要将 SectionB
项目的引用添加到 SectionA
项目,然后 using
应该可以工作。
如果在Visual Studio中打开,可以右击SectionB -> References -> Add Reference 添加selectSectionA的引用
您可以尝试打开项目的 SectrionB.csproj 文件并添加项目引用,如下所示:
<ItemGroup>
<ProjectReference Include = "../SectionA.csproj" />
</ItemGroup>
之后您就可以在您的代码中使用它了
using ....
.....