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 = fal开发者_如何学Pythonse;
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
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?
精彩评论