Bug 58454 - Cannot resolve method Subscription`1 Subscribe[TSelection](System.Func`2[TState,TSelection], System.Action`2[TSelection,TState]) because the declaring type of the method handle FSI_0002+Store`1[TState] is generic. Explicitly provide the declaring type to
Summary: Cannot resolve method Subscription`1 Subscribe[TSelection](System.Func`2[TSta...
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection (show other bugs)
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aleksey Kliger
URL:
Depends on:
Blocks:
 
Reported: 2017-07-28 12:33 UTC by Jason Imison
Modified: 2017-08-01 16:21 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
C# SRE reproduction (2.14 KB, text/vnd.trolltech.linguist)
2017-07-31 22:19 UTC, Aleksey Kliger
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jason Imison 2017-07-28 12:33:51 UTC
Original issue https://github.com/Microsoft/visualfsharp/issues/3398

Save the following file as bug.fsx

```
open System

type DemoState = { Title : string }
type AppState = { Demo : DemoState }

type Store<'TState>() =
    member this.Subscribe<'TSelection> (selector:Func<'TState, 'TSelection>) (callback:Action<'TSelection, 'TState>) =
        let c:obj * 'TState -> unit = fun (selection, state) -> callback.Invoke((selection :?> 'TSelection), state)
        let s:'TState -> obj = fun state -> (selector.Invoke state) :> obj

        let subscription = new Subscription<'TState>(this, c, s)

        subscription

and Subscription<'TState>(store:Store<'TState>, callback:obj * 'TState -> unit, selector:'TState -> obj) =
    let store = store
    member this.Callback = callback
    member this.Selector = selector

// Create the store instance
let store = Store()

// Add a subscription
let logSubscription (title:string) (state:AppState) =
    printfn "Title: %s" title
    printfn "State: %O" state

let selector = Func<AppState, string> (fun state -> state.Demo.Title)
let callback = Action<string, AppState> logSubscription

// And boom
store.Subscribe selector callback
```

run `fsharpi bug.fsx`

Actual result -

```
System.ArgumentException: Cannot resolve method Subscription`1 Subscribe[TSelection](System.Func`2[TState,TSelection], System.Action`2[TSelection,TState]) because the declaring type of the method handle FSI_0002+Store`1[TState] is generic. Explicitly provide the declaring type to GetMethodFromHandle.
  at System.Reflection.MethodBase.GetMethodFromHandle (System.RuntimeMethodHandle handle) [0x00072] in <ffb99659fc1c47faa4d5f883014d08aa>:0
  at System.Reflection.Emit.GenericTypeParameterBuilder.InternalResolve () [0x00019] in <ffb99659fc1c47faa4d5f883014d08aa>:0
  at System.Reflection.Emit.TypeBuilderInstantiation.InternalResolve () [0x0001e] in <ffb99659fc1c47faa4d5f883014d08aa>:0
  at System.Reflection.Emit.ConstructorOnTypeBuilderInst.RuntimeResolve () [0x00000] in <ffb99659fc1c47faa4d5f883014d08aa>:0
  at System.Reflection.Emit.ModuleBuilder.RuntimeResolve (System.Object obj) [0x00080] in <ffb99659fc1c47faa4d5f883014d08aa>:0
  at <StartupCode$FSI_0002>.$FSI_0002.main@ () [0x00044] in <0e5e3226a7764792a4247fc606b6c0f1>:0
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <ffb99659fc1c47faa4d5f883014d08aa>:0
```

Expected result - no exception
Comment 1 Jason Imison 2017-07-28 12:36:50 UTC
Tested with Mono 4.8.0.334 and it's working fine.
Comment 2 Aleksey Kliger 2017-07-31 22:19:23 UTC
Created attachment 23926 [details]
C# SRE reproduction

Attached pure C# reproduction.

This is a regression since a0ad8154f7cdca706802aa196d95686e0f8d3b7b

Looks like in GenericTypeParameterBuilder:InternalResolve() we need to pass the declaring type (mbuilder.TypeBuilder.InternalResolve().TypeHandle).