简体   繁体   中英

Customize/override function depending on parameter/generic

I would like to customize the function of an external class(selfwritten) depending of the function parameter / generic type by adding a new function to the class.

For example the same as here http://automapper.readthedocs.io/en/latest/Custom-value-resolvers.html#custom-constructor-methods were a custom interface is created and added to the object.

So I would like to make an own interace for a type T (class2 for example) and add this interface to class1 and when i call a function from class1, this interface/function should be used.

object1(of class1).add(Interface x, typeof(class2))
object1(of class1).DoSomething<class2>() -> calls interface/function x

the same functionality as here: (but the functions DoSomethingY is not added after an instance is created but instead needed to be in the class1 itself - which i dont want to make)

public class class1 {

  private DoSomething1..
  private DoSomething2..
  private DoSomethingDefault..

  public T DoSomething<T>()
  {
     if(typeof(t) == ...)
       return DoSomething1();
     else(typeof(T) == ..)
       return DoSomething2();
     else
       return DoSometingDefault();
  }
}

So my question. What do i use: Interaces, delegates? And where/how do i store this?

EDIT: to be a bit more clear

public interface IDoSomething<T>
{
   public T Work(string obj)
}

public DoSomething1 : IDoSomething<class2>
{
  public class2 Work()
  {
     return new class2() {name = "xx"} {
  }
}

var obj1 = new class1();
obj1.Register<class2>(DoSomething1) // might be wrong syntax, but my intention
// or obj1.Register(tyopeof(class2),DoSomething1);

class2 obj2 = obj1.DoSomething<class2>();
//obj1/class1 should call DoSomething1.Work();

class3 obj3 = obj1.DoSomething<class3>(); // should call default if not registered

is this a normal design pattern? to invoke a custom function instead of using the default class function?

If i understand you right. You need something like this:

var _do = new Do();

_do.Register<I1>(() => Console.WriteLine("I1 action"));
_do.Register<I2>(() => Console.WriteLine("I2 action"));

_do.Work<I1>();
_do.Work<I2>();
_do.Work<string>();

public class Do {
    IDictionary<Type, Action> actions = new Dictionary<Type, Action>();

    private void DefaultAction(){
        Console.WriteLine("Default action called");
    }

    public void Register<T>(Action act){
        var type = typeof(T);
        if(!actions.ContainsKey(type)){
            actions.Add(type, act);
        }
    }

    public void Work<T>() 
    {
        var type = typeof(T);
        if(actions.ContainsKey(type))
        {
            actions[type]();
        }
        else
        {
            DefaultAction();
        }
    }
}

public interface I1 { }

public interface I2 { }

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