fsprojects / FSharp.TypeProviders.SDK

The SDK for creating F# type providers
https://fsprojects.github.io/FSharp.TypeProviders.SDK/
MIT License
298 stars 94 forks source link

Accessing GetCustomAttributes<T> on a providedType/Method fails with an invalid cast exception #240

Closed 7sharp9 closed 6 years ago

7sharp9 commented 6 years ago

Description

Accessing GetCustomAttributes on a providedType/Method fails with an invalid cast exception

Repro steps

Using one of the tests as reference doing this causes the exception to occur:

let t = (tp :> ITypeProvider).ApplyStaticArguments(providedType, [| typeName |], staticArgs)

//check that attributes read using the typed overload to GetCustomAttributes<t> can be read from the provided method
let firstMethod = t.GetMembers() |> Array.find (fun m -> m :? ProvidedMethod )
let attrib = firstMethod.GetCustomAttributes<CompiledNameAttribute>()

Expected behavior

No exception

Actual behavior

Exception:

System.InvalidCastException
Specified cast is not valid.
  at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
  at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T] (System.Reflection.MemberInfo element) [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/reflection/CustomAttributeExtensions.cs:117 
  at FSharp.TypeProviders.SDK.Tests.BasicGenerativeTests.GenerativePropertyProviderWithStaticParams generates for correctly () [0x0011f] in <5b48d8756931f8a3a745038375d8485b>: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 /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Reflection/MonoMethod.cs:305

Known workarounds

None

Related information

There is a PR for a test here: https://github.com/fsprojects/FSharp.TypeProviders.SDK/pull/239

7sharp9 commented 6 years ago

PR with test now includes a fix for this issue.

7sharp9 commented 6 years ago

Fixed