user2018102
user2018102

Reputation: 13

C# Reference a property through a string returned from the Console.ReadLine

I'm new to C# and slowly learning as I go forward. In a console application I want to be able to type in the name of the property I want to display. The problem I stumble upon is that ReadLine will return a string and I do not know how to turn that string in to a reference to the actual property.

I wrote a simple example to explain what I'm trying to do. The example will now only type out whatever input it gets twice.

I have tried typeof(Person).GetProperty(property).GetValue().ToString() but all I get is an error message saying that there is no overload for GetValue that takes 0 arguments.

Thanks Rickard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AskingForHelp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.FirstName = "Mike";
            p.LastName = "Smith";
            p.Age = 33;

            p.displayInfo(Console.ReadLine());
        }
    }

    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public UInt16 Age { get; set; }

        public Person()
        {
            FirstName = "";
            LastName = "";
            Age = 0;
        }

        public void displayInfo(string property)
        {
            Console.WriteLine(property + ": " + property);
            Console.ReadKey();
        }
    }
}

Upvotes: 1

Views: 842

Answers (4)

TYY
TYY

Reputation: 2716

This will give you what you are looking for.

    static void Main(string[] args)
    {
            Person p = new Person();
            p.FirstName = "Mike";
            p.LastName = "Smith";
            p.Age = 33;
            Console.WriteLine("List of properties in the Person class");
            foreach (var pInfo in typeof (Person).GetProperties())
            {
                Console.WriteLine("\t"+ pInfo.Name);
            }
            Console.WriteLine("Type in name of property for which you want to get the value and press enter.");
            var property = Console.ReadLine();
            p.displayInfo(property);
    }


    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public UInt16 Age { get; set; }

        public Person()
        {
            FirstName = "";
            LastName = "";
            Age = 0;
        }

        public void displayInfo(string property)
        {
            // Note this will throw an exception if property is null 
            Console.WriteLine(property + ": " + this.GetType().GetProperty(property).GetValue(this, null));
            Console.ReadKey();
        }
    }

Upvotes: 0

Johnny_D
Johnny_D

Reputation: 4652

You should use smth like this:

public static object GetPropValue( object src, string propName )
 {
    return src.GetType( ).GetProperty( propName ).GetValue( src, null );
 }

As second parameter is an index: index Type: System.Object[] Optional index values for indexed properties. This value should be null for non-indexed properties.

Upvotes: 1

ppetrov
ppetrov

Reputation: 3105

you need to give the GetValue function the reference of the object that contains the property.

you need to change your displayInfo function:

public void displayInfo(string property, Person p)

then in this function you can call the GetValue function

Upvotes: 0

Anton Kovalenko
Anton Kovalenko

Reputation: 21517

GetValue needs an instance of your class to actually get value from. Like this:

typeof(Person).GetProperty(property).GetValue(this).ToString()
// to be used in a non-static method of Person

Upvotes: 0

Related Questions