Daniel Elliott
Daniel Elliott

Reputation: 22857

Is it possible to invoke a delegate in monotouch?

I have the following using System;

namespace mtSpec
{
    public class SpecElement
    {
        public SpecElement (Delegate action, string name)
        {
            Name = name;
            this.ActionToTake = action;
        }

        public Delegate ActionToTake {get; set;}
        public string Name {get; set; }

        public bool Execute()
        {
            try {
                 this.ActionToTake.DynamicInvoke();
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    }
}

Calling Execute always causes the following at the point of DynamicInvoke():

  • Assertion: should not be reached at ../../../../mono/mini/debugger-agent.c:3092

Am I missing something? How can I invoke my delegate please?

Escoz? Miguel? Anyone?

Upvotes: 1

Views: 271

Answers (2)

Robert Kozak
Robert Kozak

Reputation: 2033

Instead of Delegate I would use Action. No need to call DynamicInvoke.

public class SpecElement
{
    public SpecElement (Action action, string name)
    {
      Name = name;
    this.ActionToTake = action;
  }

    public Action ActionToTake {get; set;}
    public string Name {get; set; }

  public bool Execute()
    {
      try 
      {
       this.ActionToTake();
         return true;
      } 
      catch (Exception) 
      {
         return false;
      }
   }
}

Upvotes: 2

SLaks
SLaks

Reputation: 887405

You can just write

something();

Upvotes: 2

Related Questions