Repository navigation
Description
F# does not mark CLI event accessor methods (add_X, remove_X) with the specialname IL flag if the event member is abstract.
This can be problematic for Reflection-based tools because they might not correctly recognize event accessor methods as such.
Someone stumbled on this a while ago when using the Moq mocking library against a F#-defined interface type. In order to keep up support for F#, Moq had to stop querying method.IsSpecialName, which it used to quickly check whether it was worth trying to find a property or event associated with method (a much more expensive operation). Abandoning that check for F# also meant reduced runtime performance for all other (C#) code.
I'd like to bring back this performance optimization in Moq. Therefore it would be great if F# could correctly mark all event accessors as such in metadata.
Repro steps
Compile the following code as a console application, run it, and observe its output.
open System;
open System.Reflection
type IAbstract1 =
[<CLIEvent>]
abstract member Event : IEvent<EventHandler, EventArgs>
type IAbstract2 =
[<CLIEvent>]
abstract member Event : IDelegateEvent<Action>
[<AbstractClass>]
type Abstract3() =
[<CLIEvent>]
abstract member Event : IDelegateEvent<Action>
type Concrete1() =
let event = new Event<EventHandler, EventArgs>()
[<CLIEvent>]
member this.Event = event.Publish
type Concrete2() =
[<CLIEvent>]
member this.Event = { new IDelegateEvent<Action> with
member this.AddHandler _ = ()
member this.RemoveHandler _ = () }
[<EntryPoint>]
let main argv =
let isTestType (t: Type) =
t.Name.Contains("Abstract") || t.Name.Contains("Concrete")
let printType (t: Type) =
printfn "%s" t.Name
t.GetMethods()
|> Seq.filter (fun m -> m.Name.Contains("Event"))
|> Seq.iter (fun m -> printfn "* %s IsSpecialName = %b" m.Name m.IsSpecialName)
printfn ""
Assembly.GetExecutingAssembly().GetExportedTypes()
|> Seq.filter isTestType
|> Seq.iter printType
0Expected behavior
The program should print IsSpecialName = true for all members, i.e. including those of the abstract types IAbstract1, IAbstract2, and Abstract3.
Actual behavior
The program prints IsSpecialName = true only for accessor methods in types Concrete1 and Concrete2. The accessor methods from the abstract types do not have specialname set.
Known workarounds
None known to me. (Am I possibly missing the correct way to define a CLI event in an abstract type?)
Related information
- Visual Studio 15.8.8
- Microsoft Visual F# Tools 10.2 for F# 4.5
- tested against .NET Core 2.1 and .NET Framework 4.7.1
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNew
Activity
marklam commentedon Feb 18, 2019
I just hit this problem after updating some old test code to use the new NSubstitute (4.0), where they also added the check for the
specialnameflag.I thought at first it was maybe just a case F# not behaving like C#, but according to the ECMA-335 "Common Language Infrastructure (CLI) Partitions I to VI " spec:
1 remaining item
Complete codegen regression test suite: add remaining 32 issues
Fix stub tests #878 and #5834 with actual reproductions
Fix stub tests and add OUT_OF_SCOPE markers for feature requests
Fix #5834: Abstract event accessors now have SpecialName flag
Fix DU duplicate methods, event metadata
Fix DU duplicate methods, event metadata
Fix DU duplicate methods, event metadata
Fix DU duplicate methods, event metadata
Fix DU duplicate methods, event metadata
Fix DU duplicate methods, event metadata
Address review: remove leftover files, add IL verification tests