You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into an interesting scenario with the following code to try obtain a Gio.File from the Gio.ListModel returned by Gtk.FileChooser.GetFiles()
Gtk.FileChooserDialogfile_chooser= ...// open some files with a file chooserGio.ListModel files =file_chooser.GetFiles();for(uinti=0,n=files.GetNItems();i<n;++i){// The runtime type is just a GObject.Object instance// The gtype is 'GLocalFile' which is a private type that inherits from GObject and implements the 'GFile' interfaceGObject.Object?obj=files.GetObject(i);varfile=objasGio.File;// fails}
The GObject returned is of type GLocalFile which isn't public, so there isn't a C# type to create an instance of. The ObjectWrapper then just creates an instance of the parent class, GObject.Object in this case.
This presents a problem with trying to cast to a Gio.File interface, since the C# object doesn't have a runtime type that implements that interface
Perhaps some sort of cast function that checks the gtype is needed?
My manual workaround was to do var file = new Gio.FileHelper (files.GetObject (i).Handle, ownedRef: true), although that doesn't do any safe type checking
The text was updated successfully, but these errors were encountered:
For classes which implement interfaces inside list models this should already work For private classes which implement interfaces not.
Your suggestion using a method to cast the list items we could return an IEnumarable<T> which either casts a class to a more concrete class/interface or wraps the instance into an interface wrapper and checks the type if the gobject class is simply GObject but a more concrete type is required and confirmed by the list model.
Perhaps an additional method to get a casted single item would be helpful, too.
Yeah, getting an IEnumerable<T> that does casting if necessary sounds like a good way to go. Even for cases that don't hit this interface casting issue, it would be a more C#-style API than having to use the GetNItems() to iterate over the list
I ran into an interesting scenario with the following code to try obtain a
Gio.File
from theGio.ListModel
returned byGtk.FileChooser.GetFiles()
The GObject returned is of type
GLocalFile
which isn't public, so there isn't a C# type to create an instance of. TheObjectWrapper
then just creates an instance of the parent class,GObject.Object
in this case.This presents a problem with trying to cast to a
Gio.File
interface, since the C# object doesn't have a runtime type that implements that interfacePerhaps some sort of cast function that checks the gtype is needed?
My manual workaround was to do
var file = new Gio.FileHelper (files.GetObject (i).Handle, ownedRef: true)
, although that doesn't do any safe type checkingThe text was updated successfully, but these errors were encountered: