我试图编写一些代码,在结构体上设置一个属性(重要的是它是结构体的属性),但失败了:
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle();
PropertyInfo propertyInfo = typeof(System.Drawing.Rectangle).GetProperty("Height");
propertyInfo.SetValue(rectangle, 5, null);
高度值(由调试器报告)从未设置为任何值-它保持默认值0。
我以前进行过大量有关类的反射,并且这一点很好用。此外,我知道在处理结构时,如果要设置字段,则需要使用FieldInfo.SetValueDirect,但我不知道PropertyInfo的等效方法。