C# 扩展基类方法
我是C#的新手,正在努力理解基本概念。提前感谢您的帮助。我在下面有一些示例类(在此窗口中键入,因此可能会有一些错误),并有两个问题:C# 扩展基类方法,c#,.net,inheritance,extension-methods,C#,.net,Inheritance,Extension Methods,我是C#的新手,正在努力理解基本概念。提前感谢您的帮助。我在下面有一些示例类(在此窗口中键入,因此可能会有一些错误),并有两个问题: 是否可以调用一个派生类方法,该方法使用相同的名称执行基类方法中的代码,然后在派生类方法中执行代码?每个派生类都需要执行运行检查的基类代码,然后执行特定于其类的专用代码。我可以在基类中将RunCheck()命名为其他名称,然后在调用派生类的RunCheck()时调用它,但是我必须记住在派生类的RunCheck()上调用它 在Program.cs中,如果某个字段不在我
class baseCheck
{
public DateTime StartTime { get; set; }
public DateTime LastRun { get; set; }
public int Runs { get; set; }
//Others
public void RunCheck()
{
if (Started != null)
started = DateTime.Now;
LastRun = DateTime.Now;
Runs++;
}
}
class FileCheck : baseCheck
{
public string FileName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
class DirectoryCheck : baseCheck
{
public string DirectoryName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
}
}
//Program.cs
static void Main()
{
//Create derived class - either DirectoryCheck or FileCheck
//depending on what the user chooses.
if (Console.ReadLine()=="F")
{
FileCheck c = new FileCheck();
}
else
{
DirectoryCheck c = new DirectoryCheck();
}
PrintOutput(c);
}
private void PrintOut(What do I put here?)
{
Console.WriteLine("Started: {0}",f.StartTime)
Console.WriteLine("Directory: {0}", f.DirectoryName)
Console.WriteLine("File: {0}", f.FileName}
}
只需在
DirectoryCheck
类中调用base.RunCheck()
:
public class DirectoryCheck : baseCheck
{
public string DirectoryName { get; set; }
public void RunCheck()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
base.RunCheck();
Console.WriteLine("From DirectoryCheck");
}
}
另外,在当前实现中,您正在隐藏基类RunCheck()
方法-您应该真正覆盖它-这将基类中的方法签名更改为
public virtual void RunCheck()
并在派生类中
public override void RunCheck()
我怀疑您真正想要的是模式(NVI)-在基类中公开一个受保护的虚拟方法,子类可以重写,但基类上有一个公共方法,该方法实际上在内部调用该方法-这种方法允许您扩展调用前后的操作
在您的示例中,如下所示:
class BaseCheck
{
private DateTime Started { get; set; }
public DateTime StartTime { get; set; }
public DateTime LastRun { get; set; }
public int Runs { get; set; }
//Others
public void RunCheck()
{
if (Started != null)
Started = DateTime.Now;
LastRun = DateTime.Now;
Runs++;
CoreRun();
}
protected virtual void CoreRun()
{
}
}
public class DirectoryCheck : BaseCheck
{
public string DirectoryName { get; set; }
protected override void CoreRun()
{
//I want all the code in the base class to run plus
//any code I put here when calling this class method
Console.WriteLine("From DirectoryCheck");
}
}
在派生类中,可以使用以下命令调用基类中的方法:
public override void RunCheck()
{
base.RunCheck();
// Followed by the implementation of the derived class
}
如注释中所述,基本方法需要声明为virtual
,以允许覆盖:
public virtual void RunCheck() { ... }
对于PrintOut()方法,没有神奇的方法,但是可以让它将基类作为参数,然后测试类型
private void PrintOut(baseCheck f)
{
Console.WriteLine("Started: {0}", f.StartTime)
Console.WriteLine("Directory: {0}", f.DirectoryName)
if (check is FileCheck)
{
Console.WriteLine("File: {0}", ((FileCheck)f).FileName}
}
}
或者可以使用重载:
private void PrintOut(baseCheck f)
{
Console.WriteLine("Started: {0}", f.StartTime)
Console.WriteLine("Directory: {0}", f.DirectoryName)
}
private void PrintOut(FileCheck f)
{
PrintOut((baseCheck)f);
Console.WriteLine("File: {0}", ((FileCheck)f).FileName}
}
或者您可以将打印输出方法作为类的一部分(甚至可以使用现有的
ToString()
方法),并根据需要重写它。在派生方法中,调用“super.RunCheck()”。它将调用父类方法。我将生成BaseClass
abstract
,并使用abstract
CoreRun
isnteadvirtual
。或者另一种方法是将CheckerCore
类注入BaseCheck
类。