我有一个基类有一个将文件移动到适当的文件夹的方法。有许多不同的文件和许多不同的命名方案。移动和文件夹创建对于每个文件都是相同的,但由于文件名称不同,确定日期不同。我试图做到这一点:C#方法隐藏
public class FileBase
{
protected FileInfo _source;
protected string GetMonth()
{
// 2/3 Files have the Month in this location
// So I want this to be used unless a derived class
// redefines this method.
return _source.Name.Substring(Source.Name.Length - 15, 2);
}
public void MoveFileToProcessedFolder()
{
MoveFileToFolder(Properties.Settings.Default.processedFolder + GetMonth);
}
private void MoveFileToFolder(string destination)
{
....
}
}
public class FooFile : FileBase
{
protected new string GetMonth()
{
return _source.Name.Substring(Source.Name.Length - 9, 2);
}
}
public class Program
{
FooFile x = new FooFile("c:\Some\File\Location_20110308.txt");
x.MoveFileToProcessedFolder();
}
的问题是,在基类版本“得到月”的验证码结果被调用的“MoveFileToProcessedFolder”方法内。我认为使用'new'关键字会隐藏原始实现并允许派生实现接管。这不是正在发生的事情。很显然,我并不了解这种情况下新的目的,有没有人能帮我理解这一点?
谢谢。
这听起来完全疯了。非转义路径名称暗示您可能完全错误。 – stefan 2011-03-08 18:15:22
是的。使用FooFile x = new FooFile(@“c:\ Some \ File \ Location_20110308.txt”);只需在字符串前添加@ – 2011-03-08 18:16:24
这只是一个简化的示例。文件名不是真实的。在实际路径中转义并正确。 – Bitfiddler 2011-03-08 19:13:25