Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Reflection, Geting property


  • Please log in to reply
3 replies to this topic

#1 Saturnim

Saturnim

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 May 2011 - 08:35 AM

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

#2 Mati

Mati

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationIsrael

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

Mati

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationIsrael

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.

        /// <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 Saturnim

Saturnim

    Member

  • Members
  • PipPip
  • 12 posts

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

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.