Luke Puplett
Luke Puplett

Reputation: 45135

Why does my non-dynamic class method return dynamic

The following code results in the C# compiler returning a dynamic type, but that class and the GetUserProfileAsync method are normal, 'concrete' code.

var profile = await this.UserProfileRepository.GetUserProfileAsync(this.ViewModel[ViewModelKeys.UserGivenName]);

Intellisense on the profile variable is absent, instead the editor/compiler says:

This operation will be resolved at runtime.

What's going on?

Upvotes: 0

Views: 57

Answers (1)

Luke Puplett
Luke Puplett

Reputation: 45135

Check the type of the parameter being passed into the GetUserProfileAsync method, its probably dynamic.

The compiler cannot thus know the type and resolve the method/callsite on the UserProfileRepository class.

To fix the issue, cast the value coming from this.ViewModel to the type you expect.

var profile = await this.UserProfileRepository.GetUserProfileAsync((string)this.ViewModel[ViewModelKeys.UserGivenName]);

Now the compiler can work it all out.

Upvotes: 1

Related Questions