Hi!
I am trying to get an indexer of ArrayList by reflection. Access to indexer is by property, but I can't to find GetProperty Method. Can somebady help my? I saw using of GetProperty in this post http://forums.netdui...internal-flash/ . I am using Reflection namespace ...
Reflection, Geting property
Started by Saturnim, May 28 2011 08:35 AM
3 replies to this topic
#1
Posted 28 May 2011 - 08:35 AM
#2
Posted 28 May 2011 - 09:53 AM
I the .NETMF there's no GetProperty method, so I assume you're doing this on a standard .NET environment.
Here's an example how to do it. The ArrayList has a property named Item for accessing the arraylist.
Here's how it's done in .NETMF :
Since there's no GetProperty method in .NETMF, the GetMethod allows us to reach each property's getter and setter individually. The compiler turns each property to 2 methods named "get_propName" and "set_propName".
Here's an example how to do it. The ArrayList has a property named Item for accessing the arraylist.
static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add("a"); list.Add("b"); list.Add("c"); // Print a value : // Get the Item prop, invoke GetValue on our list ojbect and // give it the desired index in the object params in this case- 1 Console.WriteLine(list.GetType().GetProperty("Item").GetValue(list,new object[] {1})); // Set a value at index 0: list.GetType().GetProperty("Item").SetValue(list,Console.ReadLine(), new object[] {0}); }
Here's how it's done in .NETMF :
ArrayList list = new ArrayList(); list.Add("a"); list.Add("b"); list.Add("c"); // Get an Item object o = list.GetType().GetMethod("get_Item").Invoke(list, new object[] { 0 }); // Set an Item list.GetType().GetMethod("set_Item").Invoke(list, new object[] { 1, "x" });
Since there's no GetProperty method in .NETMF, the GetMethod allows us to reach each property's getter and setter individually. The compiler turns each property to 2 methods named "get_propName" and "set_propName".
Edited by Mati, 28 May 2011 - 10:00 AM.
Windows XP SP3, Q6600.
#3
Posted 28 May 2011 - 10:09 AM
Anyway, why are you using Reflection to access an ArrayList instead of just using square brackets? Reflection is less efficient and more time consuming.
If you know that the object is an ArrayList, you can cast that object into an ArrayList variable and then you'll get
natural access to it's methods and properties, which is more efficient.
If you know that the object is an ArrayList, you can cast that object into an ArrayList variable and then you'll get
natural access to it's methods and properties, which is more efficient.
/// <summary> /// Does something to an object. If the object is ArrayList, it will change it first item to X /// </summary> /// <param name="o">Any object of any type</param> static void Do(object o) { // Check that the type is indeed an arraylist if (o.GetType().Name == "ArrayList") { // Cast the object to it's real type ArrayList alist = (ArrayList)o; // ArrayList alist = o as ArrayList; also works // Now you have all the props and methods if (alist.Count > 0) { alist[0] = "X"; } } }
Windows XP SP3, Q6600.
#4
Posted 28 May 2011 - 12:05 PM
I am writing simple xml serialization which allows to transport object between full.net and mf. Array List was an eg. of object which has hiden data. Normaly Object has data in fields. But Arrays and ArrayLists have data which access is by indexer. I want to implement something general. Something to allows me to serialize object with indexers. But in this case I cant to cast, becouse I dont know type. Therefore I thought that using of accessor to indexer may be solution. So I asked about GetProperty. But probably the best solution will be casting on interface. Thank you for your reply and code examples.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users