简体   繁体   English

是否有可能从PropertyInfo获得“对象”?

[英]Is it possible to get an 'object' from a PropertyInfo?

In my precent questions, I want to retrieve some values via reflection. 在我的precent问题中,我想通过反射检索一些值。 Now I want set values to objects thanks to reflection. 现在我希望通过反射将对象设置为值。

I want to write this : 我想写这个:

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 contains a type of ColumnsCollection. PropertyInfo包含一种ColumnsCollection。 I just want to "map" my PropertyInfo to an object to define some properties after : For example : 我只是想将我的PropertyInfo“映射”到一个对象之后定义一些属性:例如:

cols[prop.Nom].Hidden = false;

Is it possible ? 可能吗 ?

Best Regards, 最好的祝福,

Florian 弗洛里安

EDIT : I tried the GenericTypeTea solution, but I have some problem. 编辑:我尝试了GenericTypeTea解决方案,但我有一些问题。 Here my code snippet : 这是我的代码片段:

        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>();

But a TargetException is Thrown 但抛出了TargetException

So you already have a PropertyInfo object that is of type ColumnsCollection ? 所以你已经有一个类型为ColumnsCollectionPropertyInfo对象?

You can get it and modify it using the following code: 您可以使用以下代码获取并修改它:

var original = GetYourObject();
PropertyInfo Info = GetYourPropertyInfo(original);
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection;

Basically, you just need to pass your original object back into the PropertyInfo 's GetValue method which will return you an object. 基本上,您只需要将原始对象传递回PropertyInfo的GetValue方法,该方法将返回一个对象。 Just cast that as the ColumnsCollection and you should be sorted. 只需将其转换为ColumnsCollection ,您应该进行排序。

UPDATE: 更新:

Based on your update, you should be doing this: 根据您的更新,您应该这样做:

object original = grille.DisplayLayout.Bands[0];
PropertyInfo info = original.GetProperty("Columns");

ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection;

You must be getting your Info PropertyInfo from an object of a different type. 您必须从不同类型的对象获取Info PropertyInfo Although I think we're fixing the wrong problem here. 虽然我认为我们在这里解决了错误的问题。 I don't understand what you're trying to achieve. 我不明白你想要实现的目标。 Why not just modify grille.DisplayLayout.Bands[0].Columns directly? 为什么不直接修改grille.DisplayLayout.Bands[0].Columns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM