简体   繁体   English

C#从通用对象/类调用不同的类方法?

[英]C# Different class methods called from a generic object/class?

I have a problem that I don't know how to attack. 我有一个我不知道如何进攻的问题。 Hopefully someone can kick me in the right direction. 希望有人可以向正确的方向踢我。 =) =)

I have created a few classes, ie Word, Excel, Microstation. 我创建了一些类,即Word,Excel,Microstation。 In these classes I have the same functions with the same name that do the same thing (but of course with different code). 在这些类中,我具有执行相同功能的相同名称的相同函数(但当然使用不同的代码)。

The input to the program (Excel add-in) is a file that can be either Word, Excel or Microstation. 程序的输入(Excel加载项)是一个文件,可以是Word,Excel或Microstation。 Depending on the file type I create an instance of the correct class (Word, Excel or Microstation). 根据文件类型,我创建正确类(Word,Excel或Microstation)的实例。

When I have created the instance I would like to call a function that will call the instantiated class functions. 创建实例后,我想调用一个函数,该函数将调用实例化的类函数。

I want to do this: 我想做这个:

public function RunTheFunctions(??? o)
{
   o.OpenFile();
   o.DoStuff();
   o.SaveFile();
}

instead of: 代替:

oWord.OpenFile();
oWord.DoStuff();
oWord.SaveFile();
oExcel.OpenFile();
oExcel.DoStuff();
oExcel.SaveFile();
oMicrostation.OpenFile();
oMicrostation.DoStuff();
oMicrostation.SaveFile();

I have tried: 我努力了:

object o;
Word oWord = new Word();
o = oWord;
o.OpenFile();

But it does not work. 但这行不通。

I hope that my question is relatively clear. 我希望我的问题比较清楚。 =) =)

Regards, S 问候,S

Create an interface with the required methods and implement it in your concrete classes: 使用所需的方法创建一个接口,并在您的具体类中实现该接口:

public interface IDocument
{
    void OpenFile();
    void DoStuff();
    void SaveFile();
}

public class Word : IDocument { .... }

public function RunTheFunctions(IDocument o)
{
    o.OpenFile();
    o.DoStuff();
    o.SaveFile();
}

Besides interface solution, which is the correct way to do it, you can also use dynamic as parameter type if .NET Framework >= 4.0. 除了接口解决方案(这是正确的方法)之外,如果.NET Framework> = 4.0,也可以使用dynamic作为参数类型。 This solution makes sense if Word, Excel and Microstation are third party classes and do not share a common interface or base class. 如果Word,Excel和Microstation是第三方类并且不共享公共接口或基类,则此解决方案很有意义。 (Actually you can use Adapter Pattern in this case and return to interface solution) (实际上,在这种情况下,您可以使用适配器模式并返回接口解决方案)

public void RunTheFunctions(dynamic o)
{
    o.OpenFile();
    o.DoStuff();
    o.SaveFile();
}

This will throw an exception at run-time if the provided object does not have a corresponding method. 如果提供的对象没有相应的方法,则会在运行时引发异常。

You can create interface, which would be implemented by your Word, Excel, Microstation classes: 您可以创建接口,该接口将由您的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);

Make an interface: 制作界面:

public interface ICommonMethods
{
void OpenFile();
void DoStuff();
void SaveFile();
}

make your classes implement it and then do: 使您的类实现它,然后执行:

ICommonMethods o = new Word();

o.OpenFile();

Assuming OpenFile() and SaveFile() are the same operation for all classes, what you need is the Template Method Pattern 假设OpenFile()SaveFile()对于所有类都是相同的操作,那么您需要的是Template Method Pattern

 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.
    }
}

Then the usage: 然后的用法:

    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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM