Rocks icon indicating copy to clipboard operation
Rocks copied to clipboard

Resolve Event Extensions Issue With Open Generics

Open JasonBock opened this issue 1 year ago • 2 comments

Open generics will be supported in 8.1.0 (issue #287) . However, there's a problem when the target type is generic, an open generic is requested, and the mock type has events. The extension methods generated need to be created in a static class that is also a top-level class, which is why these extension methods are generated in a separate class. However, with open generics, this presents a problem because the type parameters need to be specified, but there's no obvious way to do this.

Therefore, the extension methods are not generated in this scenario. Users can still use AddRaiseEvent(). However, it would be desirable to find a way to remove this limitation. It is possible that if the "extensions" feature was added in C#, it might make this possible, but there is no known delivery date of this feature (and it may never show up).

JasonBock avatar Mar 19 '24 22:03 JasonBock

.NET 10 Preview 3 came out today, and extensions are in C# 14. So, this should definitely be something to look at in the near future. Give that extension(...) is within a class itself...maybe this will work now? I'll find out!

JasonBock avatar Apr 11 '25 03:04 JasonBock

Just to clarify, I added a test in EventGeneratorTests called GenerateWithOpenGenericInTypeAsync() to illustrate the issue. In that test, the gen-d code doesn't have a static class created to raise events, so AddRaiseEvent() has to be used with the correct event name via a string. However, in GenerateWithOpenGenericInMethodAsync(), IOpenGenericAndEventAdornmentsEventExtensions is created with a RaiseMyEvent() extension method.

JasonBock avatar Apr 12 '25 01:04 JasonBock