Troubleshooting: InvalidCastException

Open
#9 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
android, csharp

Research direction

Start with the two linked Discord write-ups and the InvalidCastException example in this issue. Turn the Release-build type-linking explanation and the JavaCast() workaround into documentation, and consider the work done when a reader can diagnose this failure and apply the documented fix.

Written by the indexing model from the issue text.

Description

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;
Dominant language
No language data
Stars
4
Forks
9
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/docs-mobile

All issues in dotnet/docs-mobile

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.