【发布时间】: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