Lately I've found myself writing alot of classes similar to:
public class MyTypeCodes
{
public static MyTypeCode E = new MyTypeCode{ Desc= "EEE", Value = "E" };
public static MyTypeCode I = new MyTypeCode { Desc= "III", Value = "I" };
private static readonly Lazy<IEnumerable<MyTypeCode>> AllMyTypeCodes =
new Lazy<IEnumerable<MyTypeCode>>(() =>
{
var typeCodes = typeof(MyTypeCodes)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Where(x => x.FieldType == typeof (MyTypeCode))
.Select(x => (MyTypeCode) x.GetValue(null))
.ToList();
return typeCodes;
});
public static IEnumerable<MyTypeCode> All
{
get { return AllMyTypeCodes.Value; }
}
}
If you notice inside of new Lazy<IEnumerable<MyTypeCode>>(() =>
that I specifically need to do typeof(MyTypeCodes)
even though I'm inside of class MyTypeCodes. Is there any way I can write this without specifically need to call typeof()
for the class i'm specifically inside of? If this was a regular method I would this.GetType()
which obviously doesn't work (for some reason) with statics.