Yes exactly what I want to do :) At least for a particular class. The problem is, I create a static instance of an object but I don not use it directly. Since I do some operations in the constructor ,like adding the object to a list, the constructor must be invoked at least once before I get the list.
I guess that the compiler just optimizes the unused object.
There must be a simple solution :-/
EDIT
Ok may be I miss something. Let me post my code. I wrote a class for custom enum purpose.
public class TypeSafeEnum<TNameType, TValueType>
{
protected readonly TNameType name;
protected readonly TValueType value;
private static List<TypeSafeEnum<TNameType, TValueType>> listEnums = new List<TypeSafeEnum<TNameType, TValueType>>();
protected TypeSafeEnum(TNameType name, TValueType value)
{
this.name = name;
this.value = value;
listEnums.Add(this);
}
public TNameType Name
{
get { return name; }
}
public TValueType Value
{
get { return value; }
}
public static TypeSafeEnum<TNameType, TValueType> GetName(TNameType name)
{
TypeSafeEnum<TNameType, TValueType> tse = null;
for (int i = 0; i < listEnums.Count; i++)
{
TypeSafeEnum<TNameType, TValueType> typeSafeEnum = listEnums[i];
if (EqualityComparer<TNameType>.Default.Equals(typeSafeEnum.name, name))
{
tse = typeSafeEnum;
}
}
return tse;
}
public static TypeSafeEnum<TNameType, TValueType> GetValue(TValueType value)
{
TypeSafeEnum<TNameType, TValueType> tse = null;
for (int i = 0; i < listEnums.Count; i++)
{
TypeSafeEnum<TNameType, TValueType> typeSafeEnum = listEnums[i];
if (EqualityComparer<TValueType>.Default.Equals(typeSafeEnum.value, value))
{
tse = typeSafeEnum;
}
}
return tse;
}
public static TNameType[] GetNames()
{
TNameType[] names = new TNameType[listEnums.Count];
for (int i = 0; i < listEnums.Count; i++)
{
TypeSafeEnum<TNameType, TValueType> typeSafeEnum = listEnums[i];
names[i] = typeSafeEnum.name;
}
return names;
}
public static TValueType[] GetValues()
{
TValueType[] values = new TValueType[listEnums.Count];
for (int i = 0; i < listEnums.Count; i++)
{
TypeSafeEnum<TNameType, TValueType> typeSafeEnum = listEnums[i];
values[i] = typeSafeEnum.value;
}
return values;
}
}
public abstract class StringEnum : TypeSafeEnum<string, int>
{
protected StringEnum(string name, int value) : base(name, value)
{
}
}
public sealed class FileOptionEnum : StringEnum
{
public static readonly FileOptionEnum Name = new FileOptionEnum("Name", 0);
public static readonly FileOptionEnum Extension = new FileOptionEnum("Extension", 1);
public static readonly FileOptionEnum Size = new FileOptionEnum("Size", 2);
public static readonly FileOptionEnum LastModified = new FileOptionEnum("Last Modified", 3);
public static readonly FileOptionEnum LastOpened = new FileOptionEnum("Last Opened", 4);
public static readonly FileOptionEnum Created = new FileOptionEnum("Created", 5);
public FileOptionEnum(string name, int value) : base(name, value)
{
}
}
Here is how I use it:
// if I omit this line it returns me empty array
FileOptionEnum @enum = FileOptionEnum.Name;
string[] names = FileOptionEnum.GetNames();
cbFileOptions.Items.AddRange(names);