How do I populate a dropdownlist with enum values?

I’m using a helper that i found here to populate my SelectLists with a generic enum type, i did a little modification to add the selected value though, here’s how it looks like : public static SelectList ToSelectList<T>(this T enumeration, string selected) { var source = Enum.GetValues(typeof(T)); var items = new Dictionary<object, string>(); var displayAttributeType … Read more

Binding a ComboBox to an enum nested in a class

Another way of getting the enum values for use as a data source: <Window.Resources> <ObjectDataProvider MethodName=”GetValues” ObjectType=”{x:Type sys:Enum}” x:Key=”TestValues”> <ObjectDataProvider.MethodParameters> <w:Type2 TypeName=”w:Test+TestEnum” /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> … ItemsSource=”{Binding Source={StaticResource TestValues}}” Note that you still need the Type2Extension because of weirdness with TypeExtension and nested types. But you wouldn’t need the extra custom markup extension. This … Read more

Why Enum’s HasFlag method need boxing?

It’s worth noting that a generic HasFlag<T>(T thing, T flags) which is about 30 times faster than the Enum.HasFlag extension method can be written in about 30 lines of code. It can even be made into an extension method. Unfortunately, it’s not possible in C# to restrict such a method to only take things of … Read more

Create an abstract Enum class

Here’s how to adapt the accepted answer to the question Abstract Enum Class using ABCMeta and EnumMeta to create the kind of abstract Enum class you want: from abc import abstractmethod, ABC, ABCMeta from enum import auto, Flag, EnumMeta class ABCEnumMeta(ABCMeta, EnumMeta): def __new__(mcls, *args, **kw): abstract_enum_cls = super().__new__(mcls, *args, **kw) # Only check abstractions … Read more

Why must an enumeration’s size be provided when it is forward declared?

This has been standardized, proposal 2764: Forward declaration of enumerations (rev. 3) allowed the forward declaration of enums if you specify the underlying type, whereas before this was not possible. The main reason is that when the underlying type is not specified the size is implementation defined and can depend on the enumerator values. From … Read more

Possible to add another item to an existing enum type?

You can force new element to have the same type as the enum, but you can’t extend it in a subclass. header file: extern const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped; implementation file: const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped = 10; Make sure to give some space in case the framework is extended, so that you don’t have conflicts. This is a … Read more

Generating Enums Dynamically

What you’re trying to do doesn’t make a whole lot of sense. Enums are really only for the benefit of compile time, as they represent a fixed set of constants. At runtime, what would be the meaning of a dynamically generated enum – how would this be different from an plain object? For example: public … Read more