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

Compiler Error CS0266 and InvalidCastException with RequestMessage<T> #613

Open
llanecsi opened this issue Feb 24, 2022 · 0 comments
Open

Comments

@llanecsi
Copy link

llanecsi commented Feb 24, 2022

I am following the example to use a RequestMessage:

// Request the value from another module
User user = WeakReferenceMessenger.Default.Send<LoggedInUserRequestMessage>();

But am getting Compiler Error CS0266 which says there is no implicit conversion from RequestMessage to IMyInterface but an explicit conversion exists, am I missing a cast.

public class MyInterfaceRequestMessage : RequestMessage<IMyInterface>
{
}

IMyInterface myInterface = WeakReferenceMessenger.Default.Send<MyInterfaceRequestMessage>();

So I tried adding an explicit cast:
IMyInterface myInterface = (IMyInterface)WeakReferenceMessenger.Default.Send<MyInterfaceRequestMessage>();

But then at runtime I get an exception:
System.InvalidCastException: 'Unable to cast object of type 'MyInterfaceRequestMessage' to type 'IMyInterface'.'

This is in Visual Studio 2022 (v17.1.0)


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

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

No branches or pull requests

1 participant