[Mono-dev] Implicit Polymorphy Question

Samuel Abels newsgroups at debain.org
Thu Sep 22 17:26:46 EDT 2005


On Do, 2005-09-22 at 18:45 +0200, Robert Jordan wrote:
> You're looking for something like that (untested):
> [...]
>      return d.DynamicInvoke (args);
> [...]

Thanks, I did not know this method before. I now implemented this using
a combination of this and the Mono 2.0 generics support. Also Thanks to
JD for mentioning this.

However, the following code does not compile:

-----------------------------
using System;
using System.Collections;

public class FuncCaller<ArgType>
{
  Delegate function;
  ArgType  args;

  public FuncCaller (Delegate function_, ArgType args_)
  {
    function = function_;
    args     = args_;
  }

  public void Call ()
  {
    function.DynamicInvoke (new Object[] {args});
  }
}

public class App
{
  public delegate void EventHandler(string args);
  public static event EventHandler myEvent;

  public static void method1(ArrayList arg) { }
  public static void method2(string arg) { Console.WriteLine(arg); }

  public static void Main ()
  {
    ArrayList list = null;
    FuncCaller<ArrayList> c1 = new FuncCaller<ArrayList>(method1, list);
    FuncCaller<string>    c2 = new FuncCaller<string>   (method2, "test1");
    FuncCaller<string>    c3 = new FuncCaller<string>   (myEvent, "test2");
    c1.Call();
    c2.Call();
    c3.Call();
  }
}
-----------------------------

$ gmcs test.cs
test.cs(33) error CS1501: New invocation: Can not find a constructor in
`FuncCaller`1' for this argument list
test.cs(34) error CS1501: New invocation: Can not find a constructor in
`FuncCaller`1' for this argument list
test.cs(35) error CS1501: New invocation: Can not find a constructor in
`FuncCaller`1' for this argument list
Compilation failed: 3 error(s), 0 warnings

I am sure I am just using generics wrong, I just fail to spot the error
from looking at this

http://msdn2.microsoft.com/en-us/library/512aeb7t(en-us,vs.80).aspx

documentation.

Any hints?

Thanks,
-Samuel



More information about the Mono-devel-list mailing list