dotnet / dotnet/docs-mobile

Troubleshooting: InvalidCastException

Open
#9 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
4
Forks
9
PR merge metrics
No merged PRs in 30d

Description

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

* https://discord.com/channels/732297728826277939/732297837953679412/1164976813513130014
* https://discord.com/channels/732297728826277939/732297837953679412/1165090944505368606

Context:

App has:

```csharp
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()`:

```csharp
WrappedAdapter = _listView.Adapter.JavaCast().WrappedAdapter;
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.