访问"本"静态代码块内 [英] Access "this" inside of static codeblocks
问题描述
相似的类最近我发现自己写了很多:
公共类MyTypeCodes
{
公共静态MyTypeCode E =新MyTypeCode {说明=EEE,值=E};
公共静态MyTypeCode I =新MyTypeCode {DESC =III,值=I};
私人静态只读懒<&IEnumerable的LT; MyTypeCode>> AllMyTypeCodes =
新懒人< IEnumerable的< MyTypeCode>>(()=>
{
变种的TypeCodes = typeof运算(MyTypeCodes)
.GetFields(BindingFlags.Static |的BindingFlags。公共)
。凡(X => x.FieldType == typeof运算(MyTypeCode))
。选择(X =>(MyTypeCode)x.GetValue(NULL))
.ToList ();
返回的TypeCodes;
});
公共静态的IEnumerable< MyTypeCode>所有
{
{返回AllMyTypeCodes.Value; }
}
}
如果你里面看到新懒惰< IEnumerable的< MyTypeCode>>(()=>
,我特别需要做的 typeof运算(MyTypeCodes)
即使我'类MyTypeCodes里面米有什么方法可以让我写这篇文章没有明确需要调用 typeof运算()
该类我专门里面吗?如果这是一个普通方法我会 this.GetType()
这显然不符合静(出于某种原因)工作。
有什么办法,我可以写这篇文章没有特别需要调用的typeof()为第i类特别是里面的?
块引用>
没有,这很可能是最好的选择。如果你知道你永远不会添加任何其他领域,其他类型的,你可以跳过
其中,
条款。
此外,你应该使用
了ToList()
阻止的IEnumerable< T>
生成不必每次它枚举时间重新运行:私有静态只读懒<&IEnumerable的LT; MyTypeCode>> AllMyTypeCodes =
新懒人< IEnumerable的< MyTypeCode>>(()=>
{
返回的typeof(MyTypeCodes)
.GetFields(BindingFlags.Static | BindingFlags.Public)
//如果你不使用其他任何领域,只要跳过这
//。凡(X => x.FieldType == typeof运算(MyTypeCode))
。选择(X = GT;(MyTypeCode)x.GetValue(NULL))
.ToList();
});
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 dotypeof(MyTypeCodes)
even though I'm inside of class MyTypeCodes. Is there any way I can write this without specifically need to calltypeof()
for the class i'm specifically inside of? If this was a regular method I wouldthis.GetType()
which obviously doesn't work (for some reason) with statics.解决方案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 theIEnumerable<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(); });
这篇关于访问"本"静态代码块内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!