C#从泛型对象/类调用不同的类方法?
我有一个问题,我不知道如何攻击。希望有人能把我踢向正确的方向。=) 我创建了几个类,即Word、Excel、Microstation。 在这些类中,我使用相同名称的相同函数做相同的事情(当然,使用不同的代码) 程序(Excel加载项)的输入是一个文件,可以是Word、Excel或Microstation。 根据文件类型,我创建正确类(Word、Excel或Microstation)的实例 创建实例后,我希望调用一个函数,该函数将调用实例化的类函数 我想这样做:C#从泛型对象/类调用不同的类方法?,c#,.net,C#,.net,我有一个问题,我不知道如何攻击。希望有人能把我踢向正确的方向。=) 我创建了几个类,即Word、Excel、Microstation。 在这些类中,我使用相同名称的相同函数做相同的事情(当然,使用不同的代码) 程序(Excel加载项)的输入是一个文件,可以是Word、Excel或Microstation。 根据文件类型,我创建正确类(Word、Excel或Microstation)的实例 创建实例后,我希望调用一个函数,该函数将调用实例化的类函数 我想这样做: public function R
public function RunTheFunctions(??? o)
{
o.OpenFile();
o.DoStuff();
o.SaveFile();
}
而不是:
oWord.OpenFile();
oWord.DoStuff();
oWord.SaveFile();
oExcel.OpenFile();
oExcel.DoStuff();
oExcel.SaveFile();
oMicrostation.OpenFile();
oMicrostation.DoStuff();
oMicrostation.SaveFile();
我试过:
object o;
Word oWord = new Word();
o = oWord;
o.OpenFile();
但它不起作用
我希望我的问题比较清楚
问候,,
S使用所需的方法创建一个接口,并在具体类中实现它:
public interface IDocument
{
void OpenFile();
void DoStuff();
void SaveFile();
}
public class Word : IDocument { .... }
public function RunTheFunctions(IDocument o)
{
o.OpenFile();
o.DoStuff();
o.SaveFile();
}
您可以创建界面,该界面将由Word、Excel和Microstation类实现:
// interface for your document classes
interface IDocument
{
void OpenFile();
void DoStuff();
void SaveFile();
}
// Word implements IDocument
class Word : IDocument
{
public void OpenFile() { /* ... */ }
public void DoStuff() { /* ... */ }
public void SaveFile() { /* ... */ }
}
// later
public function RunTheFunctions(IDocument o)
{
o.OpenFile();
o.DoStuff();
o.SaveFile();
}
// usage
IDocument doc = new Word();
RunTheFunctions(doc);
制作一个界面:
public interface ICommonMethods
{
void OpenFile();
void DoStuff();
void SaveFile();
}
让您的类实现它,然后执行以下操作:
ICommonMethods o = new Word();
o.OpenFile();
除了界面解决方案(这是正确的方法)之外,如果.NET Framework>=4.0,还可以使用
dynamic
作为参数类型。如果Word、Excel和Microstation是第三方类,并且不共享公共接口或基类,则此解决方案是有意义的。(实际上,您可以在这种情况下使用,并返回到接口解决方案)
如果提供的对象没有相应的方法,这将在运行时引发异常。假设OpenFile()
和SaveFile()
对所有类都是相同的操作,那么您需要的是
然后是用法:
public void SomeFunction()
{
Document document = SelectDocument("xlsx");
document.DoStuff(); //Open file, excel operation then save file.
document = SelectDocument("docx");
document.DoStuff(); //Open file, word operation then save file.
}
public Document SelectDocument(string fileExtension)
{
switch (fileExtension)
{
case "docx": return new Word();
case "xlsx": return new Excel();
default: return null;
}
}
用常用方法定义一个接口,并让您的类实现该接口。
public abstract class Document
{
/*Assuming OpenFile and SaveFile are the same for Word, Excel and Microstation */
public void DoStuff()
{
OpenFile();
DoSpecificStuff();
SaveFile();
}
private void OpenFile()
{
//Open the file here.
}
protected abstract void DoSpecificStuff()
{
//Word, Excel and Microstation override this method.
}
private void SaveFile()
{
//Save file
}
}
public class Excel : Document
{
protected override void DoSpecificStuff()
{
//Excel does what it needs to do.
}
}
public class Word : Document
{
protected override void DoSpecificStuff()
{
//Word does what it needs to do.
}
}
public void SomeFunction()
{
Document document = SelectDocument("xlsx");
document.DoStuff(); //Open file, excel operation then save file.
document = SelectDocument("docx");
document.DoStuff(); //Open file, word operation then save file.
}
public Document SelectDocument(string fileExtension)
{
switch (fileExtension)
{
case "docx": return new Word();
case "xlsx": return new Excel();
default: return null;
}
}