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
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.
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.
GetValue(Object) Returns the property value of a specified object.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With