From 18435753035ae87539ca0da1b45074112d62e2b0 Mon Sep 17 00:00:00 2001 From: Gonzalo Paniagua Javier Date: Wed, 21 Jun 2006 15:57:26 +0000 Subject: [PATCH] 2006-06-21 Gonzalo Paniagua Javier * glade/HandlerNotFoundExeception.cs: * glade/XML.custom: provide a better error when the signature of a handler does not match the one of the event. svn path=/trunk/gtk-sharp/; revision=61911 --- ChangeLog | 6 +++ glade/HandlerNotFoundExeception.cs | 11 ++++++ glade/XML.custom | 59 +++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index d0e911f8b..ecbaa5b21 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-06-21 Gonzalo Paniagua Javier + + * glade/HandlerNotFoundExeception.cs: + * glade/XML.custom: provide a better error when the signature of a + handler does not match the one of the event. + 2006-05-10 Mike Kestner * bootstrap-2.10 : strap for the new 2.9.0 API. diff --git a/glade/HandlerNotFoundExeception.cs b/glade/HandlerNotFoundExeception.cs index b2c3d0592..528453510 100644 --- a/glade/HandlerNotFoundExeception.cs +++ b/glade/HandlerNotFoundExeception.cs @@ -52,6 +52,17 @@ namespace Glade { this.delegate_type = delegate_type; } + public HandlerNotFoundException (string message, string handler_name, string signal_name, + EventInfo evnt, Type delegate_type) + : base ((message != null) ? message : "No handler " + handler_name + " found for signal " + signal_name, + null) + { + this.handler_name = handler_name; + this.signal_name = signal_name; + this.evnt = evnt; + this.delegate_type = delegate_type; + } + protected HandlerNotFoundException (SerializationInfo info, StreamingContext context) : base (info, context) { diff --git a/glade/XML.custom b/glade/XML.custom index 879ceb7ea..68b91415f 100644 --- a/glade/XML.custom +++ b/glade/XML.custom @@ -257,12 +257,69 @@ if (!connected) { - throw new HandlerNotFoundException (handler_name, signal_name, ei, delegate_type); + string msg = ExplainError (ei.Name, delegate_type, handler_type, handler_name); + throw new HandlerNotFoundException (msg, handler_name, signal_name, ei, delegate_type); } } } } + + static string GetSignature (System.Reflection.MethodInfo method) + { + if (method == null) + return null; + + System.Reflection.ParameterInfo [] parameters = method.GetParameters (); + System.Text.StringBuilder sb = new System.Text.StringBuilder (); + sb.Append ('('); + foreach (System.Reflection.ParameterInfo info in parameters) { + sb.Append (info.ParameterType.ToString ()); + sb.Append (','); + } + if (sb.Length != 0) + sb.Length--; + + sb.Append (')'); + return sb.ToString (); + } + + static string GetSignature (Type delegate_type) + { + System.Reflection.MethodInfo method = delegate_type.GetMethod ("Invoke"); + return GetSignature (method); + } + + const System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Public | + System.Reflection.BindingFlags.Static | + System.Reflection.BindingFlags.Instance; + static string GetSignature (Type klass, string method_name) + { + try { + System.Reflection.MethodInfo method = klass.GetMethod (method_name, flags); + return GetSignature (method); + } catch { + // May be more than one method with that name and none matches + return null; + } + } + + static string ExplainError (string event_name, Type deleg, Type klass, string method) + { + if (deleg == null || klass == null || method == null) + return null; + + System.Text.StringBuilder sb = new System.Text.StringBuilder (); + string expected = GetSignature (deleg); + string actual = GetSignature (klass, method); + if (actual == null) + return null; + sb.AppendFormat ("The handler for the event {0} should take '{1}', " + + "but the signature of the provided handler ('{2}') is '{3}'\n", + event_name, expected, method, actual); + return sb.ToString (); + } System.Reflection.MemberFilter signalFilter = new System.Reflection.MemberFilter (SignalFilter);