Smart enums, inheritance and "siblings" behaving weirdly and I don't understand why.
So, I'm using
Ardalis.SmartEnum
to create some smart enums with data members.
I found myself having a lot of boilerplate, so I came up with the following code:
This code works. However, note ModeBase<TEnum> : SmartEnum<TEnum, int>
.
What I found is that this code doesn't work if I don't template ModelBase
and I don't know why.
[continues in the comments]5 Replies
This does not work:
(note that now we have
ModeBase : SmartEnum<ModeBase>, IModeInterface
)Even Visual Studio shows me a weird hint that
ChipSubMode.
and CwSubMode.
are not needed before List
.Doing this the following test is completly broken:
With the error:
System.TypeInitializationException : The type initializer for 'AetherLogger.Models.Mode' threw an exception. ----> System.NullReferenceException : Object reference not set to an instance of an object.And I don't know which bit of the language I don't understand that causes this error. I would like to learn. Thank you.
Message: System.TypeInitializationException : The type initializer for 'AetherLogger.Models.Mode' threw an exception. ----> System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: ModeTests.DifferentModesHaveDifferentSubModes() line 9 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) --NullReferenceException <>c.<GetAllOptions>b4_2(TEnum t) EnumerableSorterWith and The test still fails2.ComputeKeys(TElement[] elements, Int32 count) EnumerableSorter
1.ComputeMap(TElement[] elements, Int32 count) EnumerableSorter1.Sort(TElement[] elements, Int32 count) OrderedEnumerable
1.Fill(Buffer1 buffer, Span
1 destination) OrderedEnumerable1.ToArray() SmartEnum
2.GetAllOptions() Lazy1.ViaFactory(LazyThreadSafetyMode mode) Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) Lazy`1.CreateValue() <.cctor>b36_0() Lazy1.ViaFactory(LazyThreadSafetyMode mode) Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) Lazy1.CreateValue() SmartEnum
2.get_List() Mode.cctor() line 23
Message: System.TypeInitializationException : The type initializer for 'AetherLogger.Models.Mode' threw an exception. ----> System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: ModeTests.DifferentModesHaveDifferentSubModes() line 9 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) --NullReferenceException <>c.<GetAllOptions>b4_2(TEnum t) EnumerableSorterI also thought that probably a single member it should work, but apparently not? and2.ComputeKeys(TElement[] elements, Int32 count) EnumerableSorter
1.ComputeMap(TElement[] elements, Int32 count) EnumerableSorter1.Sort(TElement[] elements, Int32 count) OrderedEnumerable
1.Fill(Buffer1 buffer, Span
1 destination) OrderedEnumerable1.ToArray() SmartEnum
2.GetAllOptions() Lazy1.ViaFactory(LazyThreadSafetyMode mode) Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) Lazy`1.CreateValue() <.cctor>b36_0() Lazy1.ViaFactory(LazyThreadSafetyMode mode) Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) Lazy1.CreateValue() SmartEnum
2.get_List() Mode.cctor() line 23
Does solve it, yeah
Thanks for the deep dive! Wow!
What amazes me the most after all of this is that the static analyser picked up that the
.List` would be the same
This looks like it makes some use of reflection, so having all of that in the static analyser is kind of amazingZeth
REPL Result: Success
Result: ValueTuple<int, int>
Compile: 394.234ms | Execution: 39.682ms | React with ❌ to remove this embed.
ah because List is a static member and that is shared when they aren't generic, got it
VS recommended to just put
List
in there, no prefix
yes that, sorry
yes yes, that's why I'm saying, it was just "hey, this symbol is available from here"
but yeah, seems that Ardalis.SmartEnum is a smartely crafted library
pun totally intended
thanks for the time for the deep dive!