Teemee Tang

Teemee Tang

  • NA
  • 111
  • 9.8k

C# Generics Help me please

Sep 13 2017 10:52 AM
Hi Guys,
 
Will you help me on my concern? I want to perform code reuse for the snippet below using Generics. How can I turn this to a single method? I am still not that familiar with Generics.
 
 
public void ReadInputFiles(string sourcePath)
{
CompanyAddressInputFile path = new CompanyAddressInputFile();
path.Path = sourcePath;
string[] filePaths = GetFiles<string>(path.Path);


foreach (string file in filePaths)
{
CompanyAddressInputFile companyAddressInputFile = new CompanyAddressInputFile();
companyAddressInputFile.Path = sourcePath;
companyAddressInputFile.Filename = Path.GetFileName(file);

ProcessFile(companyAddressInputFile);
}
}

public void ReadOutputFiles(string desPath)
{
CompanyAddressOutputFile path = new CompanyAddressOutputFile();
path.Path = desPath;
string[] filePaths = GetFiles<string>(path.Path);


foreach (string file in filePaths)
{
CompanyAddressOutputFile companyAddressOutputFile = new CompanyAddressOutputFile();
companyAddressOutputFile.Path = desPath;
companyAddressOutputFile.Filename = Path.GetFileName(file);
UpdateAddress(companyAddressOutputFile);
}
 
 And, is below a good Generics method implementation?
 
  public string[] GetFiles<T>(T filePath)
{
string[] filePaths = Directory.GetFiles(filePath.ToString());

return filePaths;
}

Answers (5)