- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个扩展以从 Enum
中获取描述和其他属性。我见过使用 FieldInfo
的示例和其他使用 MemberInfo
的示例。谁能解释一下有什么区别,什么时候我应该使用哪一个会有所不同?
最佳答案
MemberInfo
是 FieldInfo
和 PropertyInfo
的抽象基类。因此,当您想要访问字段时,请使用 FieldInfo
,对于属性,请使用 PropertyInfo
。
编辑:要在您的枚举值上设置属性,您可以使用:
var attr = typeof(MyEnum).GetField(myEnumValue.ToString()).GetCustomAttributes(typeof(Description), false);
if (attr.Length > 0) return attr[0].Description;
在这种情况下,您还可以使用 GetMember
而不是 GetField
,因为 GetCustomAttributes
是在 MemberInfo
上定义的,因此在 FieldInfo
和 PropertyInfo
上提供。
关于c# - MemberInfo 和 FieldInfo 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912259/
我能找到的FieldInfo 文档没有比较和对比这两种方法。我有兴趣了解何时应该使用一个而不是另一个(最好使用一小段代码)。 最佳答案 SetValueDirect 采用 TypedReference
我正在尝试重用现有代码……但没有成功。这是代码片段: using System; using System.Collections.Generic; using System.Collections.
有什么方法可以获取要为该变量赋值的函数中字段的 FieldInfo 吗? 看我的例子: class SomeClass{ MyType myObject = SomeOtherClass
我想获取类的所有字段,但不获取类事件的底层实现。type.GetFields(BindingFlags...) 返回事件字段的核心委托(delegate)。有谁知道如何过滤掉它们? 最佳答案 .NET
我正在尝试从结构中获取数组值的字段信息。到目前为止,我有以下内容,但我不知道如何获取我想要的信息。 [StructLayout(LayoutKind.Sequential)] publ
你好, 我需要创建一个函数来迭代存储变量名和变量新值的字典。之后,我需要用该值更新类变量。 void UpdateValues(Type type, Dictionary values) {
FieldInfo:=Array(Array(0, 5), Array(8, 1)) 我正在尝试将文本用于固定宽度的列。 为此,我录制了一个宏。 由于我是 VBA 新手,所以我可以理解一些代码。 但是
我有以下内容: [Serializable()] public struct ModuleStruct { public string moduleId;
我正在尝试像这样使用反射来设置数组字段: FieldInfo field = ... A[] someArray = GetElementsInSomeWay(); field.SetValue(th
所以我的问题是:我有一个仅由 FieldInfo 表示的对象实例。我没有其他访问该对象的权限。是否可以像这样仅通过字段信息获取该对象实例? MyObject myObject = fieldInfo.
大家好,我需要访问类 SomeClass ,它被声明在 Wrapper 类中有一个私有(private)字段,使用反射到目前为止我已经能够获得私有(private)字段成员。我如何将它转换回其原始类型
我在这里使用 C# 反射:我有一个属性的 FieldInfo,我想获取它所属的类的实例(这样我就可以访问另一个属性的内容): 以这门课为例: class MyClass { public int
public partial class Form1 : Form { public Form1() { InitializeComponent();
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我正在开发一个项目,其中加载了 XML 中的字段名称,并返回具有该名称的字段并将其添加到当前对象。问题是 FieldInfo.GetValue 似乎返回字段的当前值,而不是引用。有什么办法可以解决这个
基本上,我有以下内容: protected static readonly FieldInfo SpecialField = FindSpecialField(); FxCop 向我提示,如果字段是可
目前我在做这样的事情: class Foo { // Declare Fields [FieldName("F_BAR")] public int? Bar; [Fiel
如果我有一堆 FieldInfo 的实例,并且我已经知道它们的 FieldType 是可以传递给 BinaryWriter.Write 的类型之一(...),我如何自动将给定对象的字段传递给 Bina
我在通过反射在变量 o 中检索我的控件 f2 时遇到问题: public partial class Form1 : Form { private Form2 f2; public F
我在自定义序列化程序中大量使用 FieldInfo.GetValue() 调用。这个功能真的很慢。 1) 是否有一种可移植的方式来加速它?我不需要通过 FieldInfo.GetValue() 实现进
我是一名优秀的程序员,十分优秀!