Need something like static inheritance in C #

I had a little design problem and would like to consult. Suppose we have the following class hierarchy:

abstract class A
{
}

class B : A
{
}

class C: A
{
}

I want both B and C to have a specific field x, so that it differs between classes, but is shared between all instances of the same class (i.e.: if b1, b2 are instances of B and c1, c2 are instances of C, then b1 .x = b2.x and c1.x = c2.x and b1.x! = C1.x). Is there an elegant way to do this, taking advantage of the fact that both B, C come from the same base class, or do I need to create a static field x in both classes?

Thanks in advance.

+3
source share
7 answers

Do you mean this?

abstract class A
{
    static Dictionary<Type, int> all_x;
    protected int X {
        get { return all_x[GetType()]; }
        set { all_x[GetType()] = value; }
    }
}

, :

abstract class A
{
    class SharedType { int x; }
    static Dictionary<Type, SharedType> all_shared;
    protected SharedType Shared {
        get
        {
            Type t = GetType();
            SharedType result;
            if (!all_shared.TryGetValue(t, out result) {
                 result = new SharedType();
                 all_shared.Add(t, result);
            }
            return result;
        }
    }
}

, , :

abstract class A
{
    class SharedType { int x; }
    static Dictionary<Type, SharedType> all_shared;
    protected SharedType Shared;
    A() {
        Type t = GetType();
        if (!all_shared.TryGetValue(t, out Shared) {
             Shared = new SharedType();
             all_shared.Add(t, Shared);
        }
    }
}
+4

, , - A , .. A<T>. B , , C.

generics, , .NET.

, , int Foo string Bar. ( ), - .

interface IAvalue
    {
        int Foo { get; set;}
        string Bar {get; set;}
    }

    struct BValue
        : IAvalue
    {

        public int Foo { get; set; }
        public string Bar { get; set; }
    }

    struct CValue
        : IAvalue
    {
        public int Foo { get; set; }
        public string Bar { get; set; }


    }

    abstract class A<T> where T : IAvalue
    {
        protected static T myValue;
    }

    class B : A<BValue>
    {
        static B()
        {
            myValue.Foo = 1;
            myValue.Bar = "text1";
        }
    }

    class C : A<CValue>
    {
        static C()
        {
            myValue.Foo = 2;
            myValue.Bar = "text2";
        }
    }
0

x? , x A "a", x B "b" .., "a", "b",.. -, :

abstract class A {
    public static int x = 1;    // Just using "int" as example.
}

class B : A {
    public static int x = 2;
}

, ( ), , "", - :

abstract class A {
    public int X { get { return this.GetType().GetHashCode(); } }
}

-, , , ?

?

0

, , :

public abstract class A
    {
        private static ConcurrentDictionary<Type, int> _typeIDs = new ConcurrentDictionary<Type, int>();
        private static int _nextID = 1;

        public int TypeID
        {
            get
            {
                return _typeIDs.GetOrAdd(this.GetType(), type => System.Threading.Interlocked.Increment(ref _nextID));
            }
        }
    }
0
public abstract class A
{
    public abstract int Value { get; }
}

public class B : A
{
    public override int Value { get { return 1; } }
}

public class C : A
{
    public override int Value { get { return 2; } }
}
0

.net-: ,.net .

, :

public abstract class A<T> where T : A<T>
{
    protected static int myVariable { get; set; }
}

:

public class B : A<B>
{
    public B()
    {
        myVariable = 1;
    }
    public int GetVariable()
    {
        return myVariable;
    }
}

public class C : A<C>
{
    public C()
    {
        myVariable = 2;
    }
    public int GetVariable()
    {
        return myVariable;
    }
}

B myVariable, C .

, Set(int a):

public void Set(int a)
{
    myVariable = a;
}

:

static void Main(string[] args)
{
    B b1 = new B();
    C c1 = new C();
    B b2 = new B();
    C c2 = new C();

    Console.Write("{0}; ", b1.GetVariable());  // 1
    Console.Write("{0}; ", b2.GetVariable());  // 1
    Console.Write("{0}; ", c1.GetVariable());  // 2
    Console.Write("{0}; ", c2.GetVariable());  // 2

    Console.WriteLine();

    c2.Set(333);

    Console.Write("{0}; ", b1.GetVariable());  // 1
    Console.Write("{0}; ", b2.GetVariable());  // 1
    Console.Write("{0}; ", c1.GetVariable());  // 333
    Console.Write("{0}; ", c2.GetVariable());  // 333

    Console.ReadLine();
}

: 1; 1; 2; 2; 1; 1; 333; 333;.

0

< Type, Integer [] > , call GetType() , . , . . , . - .

0
source

All Articles