Need to use Reflection to read a property

Posted on 2011-10-27
Last Modified: 2012-05-12
I know the name of the property as a string, as well as its return type. But I need to call that property and presume I need to use reflection a nd posssible InvokeMember to do so.

Please explain how to do this or what I actually need to do.

Question by:newbieweb
    LVL 74

    Accepted Solution

    Here's a quick example:
    using System;
    using System.Reflection;
    namespace _27419388
        class Program
            static void Main(string[] args)
                MyClass c = new MyClass() { AProperty = 99 };
                string propName = "AProperty";
                // Get the instance's Type
                Type myClassType = c.GetType();
                // Change the binding flags according to how the property is defined.
                //  Here, since AProperty is an instance property (i.e. not static),
                //  we pass BindingFlags.Instance. We also pass BindingFlags.Public
                //  since the property is a public property
                PropertyInfo propInfo = myClassType.GetProperty(propName, BindingFlags.Instance | BindingFlags.Public);
                // Get the value stored in the property
                object propertyValue = propInfo.GetValue(c, null);
                // The value is returned as type object (see above),
                //  so we call the default ToString method to print it
        public class MyClass
            public int AProperty { get; set; }

    Open in new window

    LVL 20

    Expert Comment

    Well since you mention property and reflection I am guessing you want to read the property of a page or a usercontrol otherwise I can't imagine what your involved in exactly.

    What type of object are you trying to read a property from (class, page, uc, etc)?

    Author Closing Comment

    Thanks. All set with this since I ended up not using reflection (this time).

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now