C
cody
I have a problem with following code, and it involves something similar to
the known issue in c++ programs called "static initialisation order fiasco".
I built a class that follows the "java typesave enum pattern". I made a
generic EnumWrapper class where I wanted all my enums to derive from.
The problem now is: I discovered that when I call EnumWrapper.GetByID()
the first time, the static enum members of the derived class where not
initialized yet.
public class EnumWrapper
{
class Key
{
Type type;
int val;
internal Key(Type type, int val)
{
this.type = type;
this.val = val;
}
public override int GetHashCode()
{
return this.val ^ this.type.GetHashCode();
}
public override bool Equals(object obj)
{
return ((Key)obj).val==this.val && this.type==((Key)obj).type;
}
}
static Hashtable table = new Hashtable();
int val;
string name;
protected EnumWrapper(int val, string name)
{
this.val = val;
this.name = name;
table[new Key(this.GetType(), val)] = this;
}
public static EnumWrapper GetByID(Type type, int val)
{
return (EnumWrapper)table[new Key(type, val)];
}
}
public class Person: EnumWrapper
{
public static Person Kein = new Person(0, "Kein);
public static Person Kunde = new Person(1, "Kunde");
public static Person Lieferant = new Person(2, "Lieferant");
Person(int id, string bez)
: base(id,bez)
{
}
}
the known issue in c++ programs called "static initialisation order fiasco".
I built a class that follows the "java typesave enum pattern". I made a
generic EnumWrapper class where I wanted all my enums to derive from.
The problem now is: I discovered that when I call EnumWrapper.GetByID()
the first time, the static enum members of the derived class where not
initialized yet.
public class EnumWrapper
{
class Key
{
Type type;
int val;
internal Key(Type type, int val)
{
this.type = type;
this.val = val;
}
public override int GetHashCode()
{
return this.val ^ this.type.GetHashCode();
}
public override bool Equals(object obj)
{
return ((Key)obj).val==this.val && this.type==((Key)obj).type;
}
}
static Hashtable table = new Hashtable();
int val;
string name;
protected EnumWrapper(int val, string name)
{
this.val = val;
this.name = name;
table[new Key(this.GetType(), val)] = this;
}
public static EnumWrapper GetByID(Type type, int val)
{
return (EnumWrapper)table[new Key(type, val)];
}
}
public class Person: EnumWrapper
{
public static Person Kein = new Person(0, "Kein);
public static Person Kunde = new Person(1, "Kunde");
public static Person Lieferant = new Person(2, "Lieferant");
Person(int id, string bez)
: base(id,bez)
{
}
}