简体   繁体   中英

How to have interface methods with a different number and type of parameters? C#

Let's say I have multiple classes that all have the same actions (in my case I'm creating a plugin framework that all: Load, Run, Dispose), and I want to use an interface so I can create a PluginFactory to handle the initialization. The problem is they all need wildly different data so the methods would be the same but the parameters would be different. Is there a best practice or design pattern where I can use interfaces and polymorhism while passing in different data to the methods?

Generics wouldn't necessarily solve my problem because the number and types of parameters is different for each plugin. I'm thinking of creating a container inside the methods when I implement them that retrieves the parameters from a global source and therefore I can inject different dependencies into that global source when I test but I'm not sure if that breaks any abstraction rules.

Edit:

Here's a code example:

 public interface IPlugin
    {
        void Load();
    }

    public Hubbub : IPlugin
    {
        public void Load(int individualId, IApiDataSource apiDataSource)
        {
           //code
        }
    }

public class SysEx : IPlugin
{
    Load(OAuthToken token, string connString, User user)
    {
        //code
    }
}

public class FriendPlat: IPlugin
{
    Load(string username, string password)
    {    
        //code
    }
}

You can create an interface eg ILoadArgument

Create a class per IPlugin implementation eg FriendlyPlatArgument, SysExArgument etc. These dto classes can implement ILoadArgument interface that you created. May be they all need a common property eg CreatedAt that can also goto the interface.

Now your IPlugin interface can define Load method as

Void Load(ILoadArgument arg) ;

Also you can then make it generic as:

Void Load(T arg) where T:ILoadArgument{°°°}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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