getting property value at runtime using Reflection.

Posted on 2005-05-13
Last Modified: 2010-04-16
hi guys:

I want to get a property value at runtime but the following code is production

"System.Reflection.TargetException Object does not match target type"

Type t = obj.GetType()// obj is the object whose property is required

PropertyInfo[] pi = t.GetProperties();

foreach (PropertyInfo prop in pi)
      string sName      = prop.Name;
                Type t1 = prop.PropertyType;
      object o = prop.GetValue(prop.PropertyType,null);

am I doing somethng wrong?
Question by:rxraza
    LVL 1

    Accepted Solution

    A property info object isn't specific to a particular instance, but rather to the type.  Your code is returning the properties of the type successfully, but to actually get a value, you need to specify an instance to get the value from. ('obj' in this example)

    For example:

          DateTime obj = new DateTime();
          Type t = obj.GetType(); // obj is the object whose property is required
          PropertyInfo[] pi = t.GetProperties();
          foreach (PropertyInfo prop in pi)
            string name = prop.Name;
            object o = prop.GetValue(obj, null);
            Console.WriteLine(name + " : " + o.ToString());

    This code will output:

    Date : 1/1/0001 12:00:00 AM
    Day : 1
    DayOfWeek : Monday
    DayOfYear : 1
    Hour : 0
    Millisecond : 0
    Minute : 0
    Month : 1
    Now : 5/13/2005 10:02:30 PM
    UtcNow : 5/14/2005 3:02:30 AM
    Second : 0
    Ticks : 0
    TimeOfDay : 00:00:00
    Today : 5/13/2005 12:00:00 AM
    Year : 1

    Hope this helps.
    - Justin
    LVL 1

    Expert Comment

    Was this the answer that you were looking for?

    Author Comment

    sorry I forgot to close the question.
    LVL 1

    Expert Comment

    No problem ;)

    Glad I could help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now