这是我的密码:
Enemy ble = new Enemy();
PropertyInfo prop = ble.GetType().GetProperty("x");
prop.SetValue(ble,20, null);
Console.WriteLine(prop.GetValue(ble));
class Enemy
{
public int x { get; set; } = 20;
}正如您所看到的,我有一个敌人类,我已经了解了如何找到属性"x“并将它的值更改为设置值,在我的示例20中,但是我的问题是,我如何增加或减少它的值2例如?
发布于 2018-11-08 20:07:35
您已经使用了GetValue()和SetValue(),获取它的值,添加到它,然后再次设置新的值:
prop.SetValue(ble,(int)prop.GetValue(ble) + 2, null);https://stackoverflow.com/questions/53215336
复制相似问题