- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,我需要访问类 SomeClass ,它被声明在 Wrapper 类中有一个私有(private)字段,使用反射到目前为止我已经能够获得私有(private)字段成员。我如何将它转换回其原始类型以便我可以访问它的属性和其他成员。
internal class Program
{
private static void Main(string[] args)
{
Wrapper wrap = new Wrapper
{
SOmeProperty = new SomeClass
{
Number = 007
}
};
Type type = wrap.GetType();
FieldInfo[] infos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var item in infos)
{
}
}
}
internal class SomeClass
{
public int Number { get; set; }
}
internal class Wrapper
{
private SomeClass _tempSomeObj;
public SomeClass SOmeProperty
{
get
{
return _tempSomeObj;
}
set
{
_tempSomeObj = value;
}
}
}
最佳答案
我不知道我是否理解正确的问题。你想要私有(private)字段(支持字段)的类型??
然后您可以检查 FieldInfo 的 FieldType 属性....
像这样:
internal class Program
{
#region Methods
private static void Main(string[] args)
{
var wrap = new Wrapper { SOmeProperty = new SomeClass { Number = 007 } };
Type type = wrap.GetType();
FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var fieldInfo in fieldInfos)
{
if (fieldInfo.FieldType == typeof(SomeClass))
{
Console.WriteLine("Yap!");
}
}
}
#endregion
}
internal class SomeClass
{
#region Properties
public int Number { get; set; }
#endregion
}
internal class Wrapper
{
#region Properties
public SomeClass SOmeProperty { get; set; }
#endregion
}
关于c# - 反射获取 FieldInfo 对象的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090224/
我能找到的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() 实现进
我是一名优秀的程序员,十分优秀!