简体   繁体   中英

Calling methods with different parameters

I am trying to call several methods with different parameters. For example, I have an UIElement that I want to move and remove. In its class I implement these methods:

    public void Move(params object[] args)
    {
        Point lastpoint = (Point)Convert.ChangeType(args[0], typeof(Point));
        Point newpoint = (Point)Convert.ChangeType(args[1], typeof(Point));

        double left = Canvas.GetLeft(this) + (newpoint.X - lastpoint.X);
        double top = Canvas.GetTop(this) + (newpoint.Y - lastpoint.Y);

        Canvas.SetLeft(this, left);
        Canvas.SetTop(this, top);
    }

    public void Remove(params object[] args)
    {
        Canvas parent = this.Parent as Canvas;
        parent.Children.Remove(this);
    }

where:

  • args in Move consists of two points
  • args in Remove should be null

I use a class to collect all my events from different sources (Mouse, Touch, LeapMotion ...) called EventLinker. It is quite simple as it just contains an enum:

public enum GestureKey
{
    OnClick, 
    OnDoubleClick, 
    OnLongClick,
    OnRightClick, 
    OnDoubleRightClick,
    OnLongRightClick,
    OnMove
};

That I can use in a dictionary:

private Dictionary<GestureKey, Action<object[]>> MyDictionary;

The methods Move and Remove are linked with two different gestures:

MyDictionary.Add(GestureKey.OnRightClick, Remove);
MyDictionary.Add(GestureKey.OnMove, Move);

The idea is to use this dictionary in several listeners attached to the same UIElement to be able to use the Mouse, the TouchScreen, the LeapMotion ... to control my application. To give an example, when I detect a click in my the listener for my mouse, I call the OnClick method in my dictionary:

if (MyDictionary.ContainsKey(GestureKey.OnClick))
{
    object[] args = { _lastPoint };
    Application.Current.Dispatcher.BeginInvoke(MyDictionary[GestureKey.OnClick], args);
}

It should not be a problem if all my methods would contain the same parameters number and type but here I have a conversion problem but this solution is not very clean and I'm sure there is a way to do it like I would like to.

I want to call my methods the same way even if they have a different prototype. If anyone knows how to do it, let me know !

EDIT: I think the problem is the dictionary I use to link my methods with my enum. It must contain methods with the same prototype. Is there any class I could use which could do the same thing without the same prototype problem ?

EDIT2: Ideally, I should have

    public void Move(Point lastpoint, Point newpoint)
    {
        double left = Canvas.GetLeft(this) + (newpoint.X - lastpoint.X);
        double top = Canvas.GetTop(this) + (newpoint.Y - lastpoint.Y);

        Canvas.SetLeft(this, left);
        Canvas.SetTop(this, top);
    }

    public void Remove()
    {
        Canvas parent = this.Parent as Canvas;
        parent.Children.Remove(this);
    }

The problem, I think, is the dictionary I use to link my methods to a GestureKey (see above).

private Dictionary<GestureKey, Action<object[]>> MyDictionary;

The dictionary class allows me to link an enum with any type, here an Action. I have to specify however which parameters my Action takes which is for me a dynamic value. I guess I should do something like:

private Dictionary<GestureKey, Action<TYPELIST>> MyDictionary;

But I don't know how to have that. I tried to use a List, but I have the same problem, it asks me for something static. TYPELIST should be informed dynamically. And I don't know if the Dictionary class is the right thing for that, maybe there is a better class for that.

Slightly different markup here but you'll understand it either way. The following is working for me:

    Dictionary<string, Delegate> _callbacks = new Dictionary<string, Delegate>();
    public MainWindow()
    {
        InitializeComponent();

        _callbacks.Add("move", new Action<Point, Point>(Move));
        _callbacks.Add("remove", new Action(Remove));

        Application.Current.Dispatcher.BeginInvoke(_callbacks["move"], new Point(5, 6), new Point(1, 3));
        Application.Current.Dispatcher.BeginInvoke(_callbacks["remove"]);
    }

    public void Move(Point something1, Point something2)
    {
    }

    public void Remove()
    {
    }

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