Enum “Inheritance”

后端 未结 15 1494
野性不改
野性不改 2020-11-22 07:21

I have an enum in a low level namespace. I\'d like to provide a class or enum in a mid level namespace that \"inherits\" the low level enum.

namespace low
{
         


        
相关标签:
15条回答
  • 2020-11-22 07:55

    The short answer is no. You can play a bit, if you want:

    You can always do something like this:

    private enum Base
    {
        A,
        B,
        C
    }
    
    private enum Consume
    {
        A = Base.A,
        B = Base.B,
        C = Base.C,
        D,
        E
    }
    

    But, it doesn't work all that great because Base.A != Consume.A

    You can always do something like this, though:

    public static class Extensions
    {
        public static T As<T>(this Consume c) where T : struct
        {
            return (T)System.Enum.Parse(typeof(T), c.ToString(), false);
        }
    }
    

    In order to cross between Base and Consume...

    You could also cast the values of the enums as ints, and compare them as ints instead of enum, but that kind of sucks too.

    The extension method return should type cast it type T.

    0 讨论(0)
  • 2020-11-22 07:57

    Enums are not actual classes, even if they look like it. Internally, they are treated just like their underlying type (by default Int32). Therefore, you can only do this by "copying" single values from one enum to another and casting them to their integer number to compare them for equality.

    0 讨论(0)
  • 2020-11-22 07:57

    Enums cannot be derrived from other enums, but only from int, uint, short, ushort, long, ulong, byte and sbyte.

    Like Pascal said, you can use other enum's values or constants to initialize an enum value, but that's about it.

    0 讨论(0)
  • 2020-11-22 07:59

    another possible solution:

    public enum @base
    {
        x,
        y,
        z
    }
    
    public enum consume
    {
        x = @base.x,
        y = @base.y,
        z = @base.z,
    
        a,b,c
    }
    
    // TODO: Add a unit-test to check that if @base and consume are aligned
    

    HTH

    0 讨论(0)
  • 2020-11-22 08:02

    This is what I did. What I've done differently is use the same name and the new keyword on the "consuming" enum. Since the name of the enum is the same, you can just mindlessly use it and it will be right. Plus you get intellisense. You just have to manually take care when setting it up that the values are copied over from the base and keep them sync'ed. You can help that along with code comments. This is another reason why in the database when storing enum values I always store the string, not the value. Because if you are using automatically assigned increasing integer values those can change over time.

    // Base Class for balls 
    public class BaseBall
    {
        // keep synced with subclasses!
        public enum Sizes
        {
            Small,
            Medium,
            Large
        }
    }
    
    public class VolleyBall : BaseBall
    {
        // keep synced with base class!
        public new enum Sizes
        {
            Small = BaseBall.Sizes.Small,
            Medium = BaseBall.Sizes.Medium,
            Large = BaseBall.Sizes.Large,
            SmallMedium,
            MediumLarge,
            Ginormous
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:08

    Ignoring the fact that base is a reserved word you cannot do inheritance of enum.

    The best thing you could do is something like that:

    public enum Baseenum
    {
       x, y, z
    }
    
    public enum Consume
    {
       x = Baseenum.x,
       y = Baseenum.y,
       z = Baseenum.z
    }
    
    public void Test()
    {
       Baseenum a = Baseenum.x;
       Consume newA = (Consume) a;
    
       if ((Int32) a == (Int32) newA)
       {
       MessageBox.Show(newA.ToString());
       }
    }
    

    Since they're all the same base type (ie: int) you could assign the value from an instance of one type to the other which a cast. Not ideal but it work.

    0 讨论(0)
提交回复
热议问题