[英]Is it possible to get an 'object' from a PropertyInfo?
在我的precent问题中,我想通过反射检索一些值。 现在我希望通过反射将对象设置为值。
我想写这个:
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
if (grille != null)
{
// I want to write MapPropertyInfo method
ColumnsCollection cols = MapPropertyInfo(Info);
PropertyInfo包含一种ColumnsCollection。 我只是想将我的PropertyInfo“映射”到一个对象之后定义一些属性:例如:
cols[prop.Nom].Hidden = false;
可能吗 ?
最好的祝福,
弗洛里安
编辑:我尝试了GenericTypeTea解决方案,但我有一些问题。 这是我的代码片段:
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
ColumnsCollection c = grille.DisplayLayout.Bands[0].Columns;
// Throw a not match System.Reflection.TargetException
ColumnsCollection test = Info.GetValue(c,null) as ColumnsCollection;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
但抛出了TargetException
所以你已经有一个类型为ColumnsCollection
的PropertyInfo
对象?
您可以使用以下代码获取并修改它:
var original = GetYourObject();
PropertyInfo Info = GetYourPropertyInfo(original);
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection;
基本上,您只需要将原始对象传递回PropertyInfo
的GetValue方法,该方法将返回一个对象。 只需将其转换为ColumnsCollection
,您应该进行排序。
更新:
根据您的更新,您应该这样做:
object original = grille.DisplayLayout.Bands[0];
PropertyInfo info = original.GetProperty("Columns");
ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection;
您必须从不同类型的对象获取Info
PropertyInfo
。 虽然我认为我们在这里解决了错误的问题。 我不明白你想要实现的目标。 为什么不直接修改grille.DisplayLayout.Bands[0].Columns
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.