Skip to content

Troubleshooting: InvalidCastException #9

@jonpryor

Description

@jonpryor

I wrote this up on Discord, and it should be massaged into documentation:

Context:

App has:

partial class DragListAdapter {
    public DragListAdapter(AWidget.ListView listView, LinTest.Files.DragListView element)
    {
        try
        {
            WrappedAdapter = ((AWidget.IWrapperListAdapter)_listView.Adapter).WrappedAdapter;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

In Debug config build, it runs fine. In Release config, it throws:

V mono-stdout: # jonp: ERROR: System.InvalidCastException: Specified cast is not valid.
V mono-stdout:   at DragListView.Android.Renderers.DragListAdapter..ctor (Android.Widget.ListView listView, LinTest.Files.DragListView element) [0x00070] in <15a302750fe049359ec667fe1a08adbe>:0 

TL;DR: it fails in Release because a type is linked away. The fix/workaround is to use .JavaCast<T>():

WrappedAdapter = _listView.Adapter.JavaCast<AWidget.IWrapperListAdapter>().WrappedAdapter;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions