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();
}
}
}
This will give you what you are looking for.