C#如何通过字符串获取对象属性?

C#如何通过字符串获取对象属性?,c#,object,attributes,C#,Object,Attributes,我有一个具有属性(名称)的对象(玩家) 我想让这个属性抛出一个从[CallerMemberName]得到的字符串: private void GetAttribute([CallerMemberName] string caller = "") { //Here the string caller is "Name" //Isnt there a way i can do Like when i search Controls (Controls.Find

我有一个具有属性(名称)的对象(玩家)

我想让这个属性抛出一个从[CallerMemberName]得到的字符串:

private void GetAttribute([CallerMemberName] string caller = "")
{
//Here the string caller is "Name"
//Isnt there a way i can do Like when i search Controls (Controls.Find(...))
//player.Find(caller);
}
string attributeString = "Name";


谢谢你的回答:也许你需要的是反思:

class Program
{
    static void Main()
    {

        Player player = new Player();
        player.Name = "Foo";


        string valueOfName = typeof(Player).GetProperty("Name").GetValue(player).ToString();

        //valueOfName is now Foo

        typeof(Player).GetProperty("Name").SetValue(player, "Bar");
        //player.Name is now Bar
    }
}

class Player
{
    public string Name { get; set; }
}

名称
不是属性,而是属性。不清楚如何调用
GetAttribute
,以及您遇到了什么问题。请看。Ups,是的,sry我指的是财产,不是属性。这很有帮助,但不是我想要的,我要求得到的是财产,而不是财产的价值,这样我就可以玩player.Name=“…”;我只有string属性=“Name”,就像我有string controlName=“textBox1”,然后我做Controls[]control=Controls.FInd(controlName,true),然后我就可以做control[0]。enabled=false;这样地?typeof(Player).GetProperty(“Name”).SetValue(Player,“Foo”);好极了!请注意,反射有点慢。有没有更快的方法可以做到同样的事情?
class Program
{
    static void Main()
    {

        Player player = new Player();
        player.Name = "Foo";


        string valueOfName = typeof(Player).GetProperty("Name").GetValue(player).ToString();

        //valueOfName is now Foo

        typeof(Player).GetProperty("Name").SetValue(player, "Bar");
        //player.Name is now Bar
    }
}

class Player
{
    public string Name { get; set; }
}