我正在尝试编写一些代码来设置结构上的属性(重要的是它是结构上的属性)并且它失败了:
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 的等价物。