Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my precent questions, I want to retrieve some values via reflection. 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. I just want to "map" my PropertyInfo to an object to define some properties after : For example :

cols[prop.Nom].Hidden = false;

Is it possible ?

Best Regards,

Florian

EDIT : I tried the GenericTypeTea solution, but I have some problem. 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

like image 986
Florian Avatar asked Jul 05 '10 13:07

Florian


People also ask

How do I get a property Type from PropertyInfo?

You can do this by getting an array of all properties from the Type. GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type. GetProperty method and specifying the property name.

What is PropertyInfo?

The PropertyInfo class discovers the attributes of a property and provides access to property metadata. The PropertyInfo class is very similar to the FieldInfo class and also contains the ability to set the value of the property on an instance.

What does GetValue return c#?

GetValue(Object) Returns the property value of a specified object.

How to use GetProperty in c#?

GetProperties() Method Syntax: public System. Reflection. PropertyInfo[] GetProperties (); Return Value: This method returns an array of PropertyInfo objects representing all public properties of the current Type or an empty array of type PropertyInfo if the current Type does not have public properties.


1 Answers

So you already have a PropertyInfo object that is of type ColumnsCollection?

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. Just cast that as the ColumnsCollection and you should be sorted.

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. 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?

like image 149
djdd87 Avatar answered Oct 18 '22 17:10

djdd87