ホームページ  >  記事  >  バックエンド開発  >  C# はリフレクションを使用して読み取り専用プロパティに値を割り当てることができますか?

C# はリフレクションを使用して読み取り専用プロパティに値を割り当てることができますか?

黄舟
黄舟オリジナル
2017-02-17 10:54:511630ブラウズ

結論: 次のようにデモを検証できます:

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 に使用しても効果がないのでしょうか?

上記のコードを次の 2 つの文に分割します:

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

単一ポイント追跡では、次のことがわかります:


PropertyDe を取得した後スクリプターは、抽象クラス インスタンスの後、SetValue メソッドを呼び出すと、そのサブクラス ReflectPropertyDescriptor から呼び出されます。



具体的な実装はサブクラスReflectPropertyDescriptorにあり、Microsoftソースコードから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);
                        }
                    }
                }
            }
        }

コードから確認できますout の場合、読み取り専用属性は直接スキップされます。 。 。 。 。 。

それでは、PropertyInfo には何か制限があるのでしょうか?

PropertyInfo によって呼び出される SetValue は次のとおりです:



具体的な実装は、次の Microsoft のオープン ソース コードにあります:

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

まだ見られません

物件情報

SetValue


PropertyInfo.GetSetMethodメソッド(Boolean)の制限は何ですか


上記は方法ですC# はリフレクションを使用して値を割り当てることができます読み取り専用プロパティに?関連コンテンツの詳細については、PHP 中国語 Web サイト (www.php.cn) に注目してください。



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。