Open softwarekamal opened 2 years ago
At a glance,
If I have following classes:
class BaseClass : SmartEnum<>{
}
class ClassOne : BaseClass{
public static readonly A = new ClassOne("A", 1);
}
class ClassTwo : BaseClass{
public static readonly A = new ClassOne("A", 1);
public static readonly B = new ClassOne("B", 1);
}
When I access to ClassTwo.List I should get A, B When I access to ClassOne.List I should get A only but I got exception (duplicate key)!,
I think the name should not treated as duplicated (the Literial A
), because ClassOne are different than ClassTwo
But It should be duplicated if BaseClass
contains A
literal.
Now I don't know what should I do exactly, The SmartEnum.List
should treated in target class and their childs only.
If I call ClassTwo.List
I should get BaseClass
literals and ClassTwo
literals only!! not ClassOne
literals.
Please @ardalis, Can you help on that to enhance SmartEnum.List
or guide me to correct my scenario.
Thank you dear!!
Why do you want to inherit both from the same base class? SmartEnum, as its name suggests, tries to simulate how enums work, so if basically you cannot have any derivatives of it having the same name or value. Because when given a name or a value, SmartEnum should be able to figure out which object to instantiate. In your case, if 'A' or 1 is passed in, there is ambiguity. To handle that at a design level, the library internally uses a dictionary, which is the most appropriate structure for the job.
Hello @ardalis, Your library are very awesome and incredible! I got a little problem here when try to accessing enum List property. Actually property works as static and it filled with all enums value of all classes hierarchy not the one that am target!
I think Items dictionary need to be enhancing. Because In my case I got exception "An item with the same key has already been added."
I have following scenario:
AppOneLicense:License
AppTwoLicense:License
An enum
abstract License class
used as base ofsealed AppOne, AppTwo classes
Now when AppOne have enum with value of (1 or StandardLicense) And AppTwo have enum with value (1 or TrialLicense)
Exception thrown. Because flags are the same...
Here's implementation you can try it:
That's a problem occurred :sob: The property
List
need to determine a deal with calling class hierarchy level itself. Not just static base oneSmartEnum<>
, When I need to accessMyLicenseEditionAppOne.List
it need to return a list ofAppOne
enums only not whole SmartEnum<> classes I think...Interanlly I think
List
calling _fromName dictionary. which take name as a key! So, If MyLicenseEditionAppOne and MyLicenseEditionAppTwo have same string key (ExampleStandard
name). the Exception thrown then. Maybe you can enhance the _fromName to take a tuple of <(Type, String), TEnum>.I need to overcome that issue (maybe from my side classes above need to re-design.. to accept same name in dervied enums)