Access “this” inside of static codeblocks

半世苍凉 提交于 2019-12-22 17:04:11

问题


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.


回答1:


Is there any way I can write this without specifically need to call typeof() for the class i'm specifically inside of?

No, this is likely the best option. If you know you are never going to add any other fields, of any other types, you can just skip the Where clause.

In addition, you should use ToList() to prevent the IEnumerable<T> generated from having to rerun each time it's enumerated:

private static readonly Lazy<IEnumerable<MyTypeCode>> AllMyTypeCodes =
    new Lazy<IEnumerable<MyTypeCode>>(() =>
    {
        return typeof(MyTypeCodes)
             .GetFields(BindingFlags.Static | BindingFlags.Public)
             // If you're not using any other fields, just skip this
             // .Where(x => x.FieldType == typeof (MyTypeCode ))
             .Select(x => (MyTypeCode) x.GetValue(null))
             .ToList();                 
    });



回答2:


Make a private constructor that gives you a (privately held) reference.

private MyTypeCodes() { }
private static MyTypeCodes _instance = new MyTypeCodes();

public static DoSomethingWithType()
{
  return _instance.GetType().foo();
}

Or you could have the constructor call GetType() and just use that instead of calling _instance.GetType() every time you need it.




回答3:


If you prefer the semantics, you could do:

private static readonly Type _thisType = typeof(MyTypeCodes);

// use like
var typeCodes = _thisType.GetFields(...


来源:https://stackoverflow.com/questions/19324419/access-this-inside-of-static-codeblocks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!