Can C # Use reflection to assign values to read-only properties?

Source: Internet
Author: User
Conclusion: You can

Verify the demo as follows:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;namespace icontest{public    partial class Form2: Form    {public        Form2 ()        {            InitializeComponent ();            Reflecttest RT = new Reflecttest ();            Rt. GetType (). GetProperty ("ID"). SetValue (RT, "Guid", null);            MessageBox.Show (rt.id);        }    }    public class Reflecttest    {        private string ID;        [ReadOnly (True)]        public string ID        {            get            {                return ID;            }            Set            {                id = value;            }        }}}    


Run the WinForm program output:



Small bet

Typedescriptor.getproperties used to SetValue this has no effect:

Typedescriptor.getproperties (RT) ["ID"]. SetValue (RT, "Guid");

So why is typedescriptor.getproperties used to SetValue without effect?

Split the above code into the following two sentences:

PropertyDescriptor prop = typedescriptor.getproperties (RT) ["ID"];p rop. SetValue (RT, "Guid");

Single-point tracking in, you can find:



After obtaining an instance of this abstract class of PropertyDescriptor, it is called from its subclass Reflectpropertydescriptor when the SetValue method is called.



and the specific implementation is in the sub-category: Reflectpropertydescriptor, from the Microsoft source code to find Reflectpropertydescriptor and SetValue

 public override void SetValue (object component, Object value) {#if DEBUG if (propdescusageswitch.traceverbose)                {String compname = "(null)";                String valName = "(null)"; if (component! = NULL) Compname = component.                ToString (); if (value = null) ValName = value.                ToString ();            Debug.WriteLine ("[" + Name + "]: SetValue (" + Compname + "," + ValName + ")");                } #endif if (component! = null) {ISite site = getsite (component);                IComponentChangeService changeservice = null;                Object oldValue = null;                Object Invokee = Getinvocationtarget (Componentclass, component); Debug.Assert (!                IsReadOnly, "SetValue attempted on the Read-only property [" + Name + "]"); if (!          IsReadOnly) {//announce that we is about-to-change this component//          if (site! = null) {Changeservice = (icomponentchangeservice) site.                        GetService (typeof (IComponentChangeService)); Debug.Assert (!compmodswitches.commondesignerservices.enabled | |                    Changeservice = null, "IComponentChangeService not Found"); }//Make sure this it is OK to send the onchange events//if (c Hangeservice = null) {OldValue = Securityutils.methodinfoinvoke (Getmethodvalue, Invokee, (object[                         ]) null);                        try {changeservice.oncomponentchanging (component, this); } catch (Checkoutexception Coex) {if (Coex = = Checkoutexception.cancele                            d) {return;                        } throw coEx; }} try {                        try {securityutils.methodinfoinvoke (Setmethodvalue, Invokee, New objec                            T[] {value});                        OnValueChanged (Invokee, eventargs.empty); } catch (Exception t) {//Give ourselves a chance to unwind properly is                            Fore Rethrowing the exception.                                                        value = OldValue;  If There is a problem setting the Controls property and we get://ArgumentException (from                            Properties set method)//==> becomes inner exception of targetinvocationexception ==> caught here if (T is targetinvocationexception && t.innere                                Xception! = null) {//Propagate the original exception upThrow t.innerexception;                            } else {throw t; }}} finally {//now notify the Chan                        GE service that is successful. if (changeservice! = null) {changeservice.oncomponentchanged (Compone                        NT, this, OldValue, value); }                    }                }            }        }

As you can see from the code, the read-only attribute is skipped directly ...

So what are the restrictions on PropertyInfo?

The SetValue of the PropertyInfo call is as follows:


In Microsoft Open Source code can find its specific implementation is as follows:

 [Debuggerstepthroughattribute] [Diagnostics.debuggerhidden] #if!        FEATURE_CORECLR [Targetedpatchingoptout ("performance critical to inline across NGen image boundaries")] #endif                    public override void SetValue (object obj, object value, object[] index) {SetValue (obj, Value, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |        BindingFlags.Static, NULL, index, NULL); } [Debuggerstepthroughattribute] [Diagnostics.debuggerhidden] public override void SetValue (Object ob                         J, Object Value, BindingFlags invokeattr, Binder binder, object[] index, CultureInfo culture) {            MethodInfo m = GetSetMethod (true);            if (M = = null) throw new ArgumentException (System.Environment.GetResourceString ("arg_setmethnotfnd"));            object[] args = null; if (Index! = null) {args = new object[index.                Length + 1]; for (int i=0;i<index.                length;i++) Args[i] = Index[i]; Args[index.            Length] = value;                } else {args = new object[1];            Args[0] = value;        } m.invoke (obj, invokeattr, binder, args, culture); }

not seen at the moment called by the PropertyInfo What are the limitations of SetValue?



Propertyinfo.getsetmethod Method (Boolean)



The above is that C # can use reflection to assign a value to a read-only property? For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.