Skip to main content

๐Ÿ‘ฉโ€๐Ÿ’ป ๅๅฐ„้กžๅˆฅๅฑฌๆ€ง๏ผŒๅ–ๅ€ผ่ˆ‡ๆ”นๅ€ผ

void Main()
{
string jsonKey ="Email";
var user = new User();
user.Name = "Dog";
user.Email = "Dog@gmail.com";

Type t = user.GetType();
PropertyInfo[] pArray = t.GetProperties();
Console.WriteLine(pArray.Select(x => x.Name));

string emailKey = pArray.Where(x=>x.Name == jsonKey).Select(x => x.Name).First();
Console.WriteLine(emailKey);

// ๅ–ๅ€ผ
var emailValue = t.GetProperty(emailKey).GetValue(user);
Console.WriteLine(emailValue);

// ๆ”นๅ€ผ
t.GetProperty(emailKey).SetValue(user,"Hi");
Console.WriteLine(user.Email);
}

public class User
{
public string Name {get;set;}
public string Email {get;set;}
}