Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface casting issues when an unknown type is returned #838

Open
cameronwhite opened this issue Mar 27, 2023 · 3 comments · May be fixed by #1118
Open

Interface casting issues when an unknown type is returned #838

cameronwhite opened this issue Mar 27, 2023 · 3 comments · May be fixed by #1118
Milestone

Comments

@cameronwhite
Copy link
Contributor

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.FileChooserDialog file_chooser = ... // open some files with a file chooser

Gio.ListModel files = file_chooser.GetFiles ();
for (uint i = 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' interface
	GObject.Object? obj = files.GetObject (i);
        var file = obj as Gio.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

@badcel badcel added this to the 0.7.0 milestone Mar 27, 2023
@badcel
Copy link
Member

badcel commented Mar 27, 2023

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.

@cameronwhite
Copy link
Contributor Author

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

@badcel
Copy link
Member

badcel commented Sep 10, 2024

#588 will help with integrating native types with some dotnet interfaces.

To return the correct type from a given pointer the type detection will be improved as part of #1118.

@badcel badcel modified the milestones: 0.7.0, 0.6.0 Sep 10, 2024
@badcel badcel linked a pull request Sep 22, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants