反射型GET对象字段信息的? [英] Reflection get type of FieldInfo object?
本文介绍了反射型GET对象字段信息的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要访问类的 SomeClass的这是宣布在包装类的私人领域,使用反射到目前为止,我已经能够得到私有字段成员。我该如何将它转换回原来的类型,这样我可以访问它的属性和其他成员。
内部类节目
{
私有静态无效的主要(字串[] args)
{
包装保鲜膜=新的包装
{
SOmeProperty =新SomeClass的
{$ b $数量为b = 007
}
};
$ B $型B型= wrap.GetType();
的FieldInfo []相关信息= type.GetFields(BindingFlags.NonPublic可| BindingFlags.Instance);
的foreach(在相关信息VAR项)
{
}
}
}
内部类SomeClass的
{
公众诠释号码{搞定;组; }
}
内部类包装
{
私人SomeClass的_tempSomeObj;
公共SomeClass的SOmeProperty
{
得到
{
返回_tempSomeObj;
}
组
{
_tempSomeObj =价值;
}
}
}
解决方案
我不知道如果我理解正确的问题。你想要的私人领域(支持字段)的类型?
然后,你可以检查字段信息的财产的FieldType ....
是这样的:
内部类节目
{
#地区的方法
私有静态无效的主要(字串[]参数)
{
VAR包=新的包装{SOmeProperty =新SomeClass的{数= 007}}; $ B $型B型= wrap.GetType();
的FieldInfo [] = fieldInfos type.GetFields(BindingFlags.NonPublic可| BindingFlags.Instance);
的foreach(在fieldInfos VAR字段信息)
{
如果(fieldInfo.FieldType == typeof运算(SomeClass的))
{
Console.WriteLine(邑!) ;
}
}
}
#endregion
}
内部类SomeClass的
{
#区域属性
公众诠释号码{搞定;组; }
#endregion
}
内部类包装
{
#区域属性
公共SomeClass的SOmeProperty {搞定;组; }
#endregion
}
HI All, I need to access the class SomeClass which is declared has a private field in the Wrapper class, using Reflection so far i have been able to get private field members . How do i cast it back to its original type so that i could access it properties and other members.
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;
}
}
}
解决方案
I dont know if i understand the question correct. You want the type of the private field (backing field)??
Then you could check the FieldType property of the FieldInfo....
like this:
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
}
这篇关于反射型GET对象字段信息的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文