C# 可以利用反射給唯讀屬性賦值嗎?

來源:互聯網
上載者:User
結論:可以

驗證demo如下:

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;            }        }    }}


運行winform程式輸出:



小註:

TypeDescriptor.GetProperties用來setvalue這沒有作用:

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

那麼為什麼TypeDescriptor.GetProperties用來setvalue沒有效果呢?

將上面的代碼拆成如下兩句:

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

單點跟蹤進去,可以發現:



在擷取到PropertyDescriptor這個抽象類別的執行個體後,在調用SetValue方法的時候,是從其子類ReflectPropertyDescriptor調用的。



而具體的實現是在子類:ReflectPropertyDescriptor中,從微軟源碼中找到ReflectPropertyDescriptor及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 read-only property [" + Name + "]");                if (!IsReadOnly) {                    // Announce that we are 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 that it is ok to send the onchange events                    //                    if (changeService != null) {                        oldValue = SecurityUtils.MethodInfoInvoke(GetMethodValue, invokee, (object[])null);                         try {                            changeService.OnComponentChanging(component, this);                        }                        catch (CheckoutException coEx) {                            if (coEx == CheckoutException.Canceled) {                                return;                            }                            throw coEx;                        }                    }                    try {                        try {                            SecurityUtils.MethodInfoInvoke(SetMethodValue, invokee, new object[] { value });                            OnValueChanged(invokee, EventArgs.Empty);                        }                        catch (Exception t) {                            // Give ourselves a chance to unwind properly before rethrowing the exception.                            //                            value = oldValue;                                                        // If there was a problem setting the controls property then we get:                            // ArgumentException (from properties set method)                            // ==> Becomes inner exception of TargetInvocationException                            // ==> caught here                            if (t is TargetInvocationException && t.InnerException != null) {                                // Propagate the original exception up                                throw t.InnerException;                            }                            else {                                throw t;                            }                        }                    }                    finally {                        // Now notify the change service that the change was successful.                        //                        if (changeService != null) {                            changeService.OnComponentChanged(component, this, oldValue, value);                        }                    }                }            }        }

從代碼中可以看出來,唯讀屬性直接被跳過去了。。。。。。

那麼PropertyInfo有沒有什麼限制呢?

PropertyInfo調用的SetValue如下所示:


在微軟開源的代碼中可以找到其具體實現如下:

 [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 obj, 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);        }

暫時沒有看到PropertyInfo調用的SetValue有什麼限制



PropertyInfo.GetSetMethod 方法 (Boolean)



以上就是C# 可以利用反射給唯讀屬性賦值嗎?的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.