确定 FieldInfo 是否是编译器生成的后台字段 [英] Determine if FieldInfo is compiler generated backingfield
本文介绍了确定 FieldInfo 是否是编译器生成的后台字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标题几乎说明了一切,我怎么知道我是否正在为 {get;设置;} 属性?
The title pretty much says it all, how do I know if I'm getting a compiler generated backingfield for a {get; set;} property ?
我正在运行此代码以获取我的 FieldInfos:
I'm running this code to get my FieldInfos:
Class MyType
{
private int foo;
public int bar {get; private set; }
}
Type type = TypeOf(MyType);
foreach (FieldInfo fi in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.NonPublic))
{
// Gets both foo and bar, however bar is called <bar>k__backingfield.
}
所以问题是,我能否以某种方式检测到 FieldInfo 是一个后台字段,而不依赖于检查其名称?(这是相当无证的,可能会在框架的下一个版本中被破坏)
so the question is, can I somehow detect that the FieldInfo is a backingfield, without relying on checking its name ? (Which is pretty undocumented, and could be broken in next version of the framework)
推荐答案
检查 .IsDefined(typeof(CompilerGeneratedAttribute), false);
对它们.
这篇关于确定 FieldInfo 是否是编译器生成的后台字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文