[Gtk-sharp-list] System.InvalidCastException from generated code

Ben Maurer bmaurer@users.sourceforge.net
Sun, 25 Jan 2004 21:25:45 -0500


Hello,

While I was playing around with gtksourceview today, i found that
placing a mark in the text would create an error. The error that was
thrown was:

> Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type
> in <0x00085> Gtk.SourceBuffer:CreateMarker (string,string,Gtk.TextIter)

Looking into the code, i found that the generator made the code:


> 		public Gtk.SourceMarker CreateMarker(string name, string type, Gtk.TextIter where) {
> 			IntPtr raw_ret = gtk_source_buffer_create_marker(Handle, name, type, ref where);
> 			Gtk.SourceMarker ret = (Gtk.SourceMarker) GLib.Object.GetObject(raw_ret);
> 			return ret;
> 		}


I placed a Console.WriteLine in there to see the type of the object that
was coming out of the cache. It was a TextIter.

I think the problem comes from the folowing C code:


>  33             GType 
>  34             gtk_source_marker_get_type (void)
>  35             {
>  36             	static GType our_type = 0;
>  37             
>  38             	/* this function's solely purpose is to allow us to later
>  39             	 * derive GtkSourceMarker from other base object and not break
>  40             	 * ABI compatibility (since this way the type is resolved at
>  41             	 * runtime) */
>  42             	if (!our_type) {
>  43				our_type = GTK_TYPE_TEXT_MARK;

It seems that Gtk# is confused as to the type of the object. What would
be the best way to fix this?