当前位置:   article > 正文

C# 可以利用反射给只读属性赋值吗?_c# 反射修改只读属性

c# 反射修改只读属性

结论:可以

验证demo如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace IconTest
  10. {
  11. public partial class Form2 : Form
  12. {
  13. public Form2()
  14. {
  15. InitializeComponent();
  16. ReflectTest rt = new ReflectTest();
  17. rt.GetType().GetProperty("ID").SetValue(rt, "Guid", null);
  18. MessageBox.Show(rt.ID);
  19. }
  20. }
  21. public class ReflectTest
  22. {
  23. private string id;
  24. [ReadOnly(true)]
  25. public string ID
  26. {
  27. get
  28. {
  29. return id;
  30. }
  31. set
  32. {
  33. id = value;
  34. }
  35. }
  36. }
  37. }

运行winform程序输出:


小注:

        TypeDescriptor.GetProperties用来setvalue这没有作用:

TypeDescriptor.GetProperties(rt)["ID"].SetValue(rt, "Guid");  

那么为什么TypeDescriptor.GetProperties用来setvalue没有效果呢?

将上面的代码拆成如下两句:

  1. PropertyDescriptor prop = TypeDescriptor.GetProperties(rt)["ID"];
  2. prop.SetValue(rt, "Guid");
单点跟踪进去,可以发现:


在获取到PropertyDescriptor这个抽象类的实例后,在调用SetValue方法的时候,是从其子类ReflectPropertyDescriptor调用的。



而具体的实现是在子类:ReflectPropertyDescriptor中,从微软源码中找到ReflectPropertyDescriptor及SetValue

  1. public override void SetValue(object component, object value) {
  2. #if DEBUG
  3. if (PropDescUsageSwitch.TraceVerbose) {
  4. string compName = "(null)";
  5. string valName = "(null)";
  6. if (component != null)
  7. compName = component.ToString();
  8. if (value != null)
  9. valName = value.ToString();
  10. Debug.WriteLine("[" + Name + "]: SetValue(" + compName + ", " + valName + ")");
  11. }
  12. #endif
  13. if (component != null) {
  14. ISite site = GetSite(component);
  15. IComponentChangeService changeService = null;
  16. object oldValue = null;
  17. object invokee = GetInvocationTarget(componentClass, component);
  18. Debug.Assert(!IsReadOnly, "SetValue attempted on read-only property [" + Name + "]");
  19. if (!IsReadOnly) {
  20. // Announce that we are about to change this component
  21. //
  22. if (site != null) {
  23. changeService = (IComponentChangeService)site.GetService(typeof(IComponentChangeService));
  24. Debug.Assert(!CompModSwitches.CommonDesignerServices.Enabled || changeService != null, "IComponentChangeService not found");
  25. }
  26. // Make sure that it is ok to send the onchange events
  27. //
  28. if (changeService != null) {
  29. oldValue = SecurityUtils.MethodInfoInvoke(GetMethodValue, invokee, (object[])null);
  30. try {
  31. changeService.OnComponentChanging(component, this);
  32. }
  33. catch (CheckoutException coEx) {
  34. if (coEx == CheckoutException.Canceled) {
  35. return;
  36. }
  37. throw coEx;
  38. }
  39. }
  40. try {
  41. try {
  42. SecurityUtils.MethodInfoInvoke(SetMethodValue, invokee, new object[] { value });
  43. OnValueChanged(invokee, EventArgs.Empty);
  44. }
  45. catch (Exception t) {
  46. // Give ourselves a chance to unwind properly before rethrowing the exception.
  47. //
  48. value = oldValue;
  49. // If there was a problem setting the controls property then we get:
  50. // ArgumentException (from properties set method)
  51. // ==> Becomes inner exception of TargetInvocationException
  52. // ==> caught here
  53. if (t is TargetInvocationException && t.InnerException != null) {
  54. // Propagate the original exception up
  55. throw t.InnerException;
  56. }
  57. else {
  58. throw t;
  59. }
  60. }
  61. }
  62. finally {
  63. // Now notify the change service that the change was successful.
  64. //
  65. if (changeService != null) {
  66. changeService.OnComponentChanged(component, this, oldValue, value);
  67. }
  68. }
  69. }
  70. }
  71. }

从代码中可以看出来,只读属性直接被跳过去了。。。。。。

那么PropertyInfo有没有什么限制呢?

PropertyInfo调用的SetValue如下所示:


在微软开源的代码中可以找到其具体实现如下:

  1. [DebuggerStepThroughAttribute]
  2. [Diagnostics.DebuggerHidden]
  3. #if !FEATURE_CORECLR
  4. [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
  5. #endif
  6. public override void SetValue(Object obj, Object value, Object[] index)
  7. {
  8. SetValue(obj,
  9. value,
  10. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static,
  11. null,
  12. index,
  13. null);
  14. }
  15. [DebuggerStepThroughAttribute]
  16. [Diagnostics.DebuggerHidden]
  17. public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
  18. {
  19. MethodInfo m = GetSetMethod(true);
  20. if (m == null)
  21. throw new ArgumentException(System.Environment.GetResourceString("Arg_SetMethNotFnd"));
  22. Object[] args = null;
  23. if (index != null)
  24. {
  25. args = new Object[index.Length + 1];
  26. for(int i=0;i<index.Length;i++)
  27. args[i] = index[i];
  28. args[index.Length] = value;
  29. }
  30. else
  31. {
  32. args = new Object[1];
  33. args[0] = value;
  34. }
  35. m.Invoke(obj, invokeAttr, binder, args, culture);
  36. }

暂时没有看到PropertyInfo调用的SetValue有什么限制


PropertyInfo.GetSetMethod 方法 (Boolean)



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/773827
推荐阅读
相关标签
  

闽ICP备14008679号