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

简介: 结论:可以 验证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

结论:可以

验证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)



目录
相关文章
|
3月前
|
设计模式 C#
C#反射机制实现开闭原则的简单工厂模式
C#反射机制实现开闭原则的简单工厂模式
18 0
|
4月前
|
存储 C# 开发者
C# | 通过反射将对象属性展示在TreeView中
在编程过程中,我们经常需要处理复杂的对象和数据结构。将这些数据结构展示在UI界面上是很有用的,可以帮助开发者更好地理解和分析数据。 而TreeView作为一种常见的控件,可以以树状结构的方式展示数据,非常适合用于展示层次化的对象。 本文将介绍如何使用TreeView控件展示一个对象,并且可以动态处理对象中的属性和子对象。通过本文的学习,您将学会如何更好地理解和展示数据,提高编程效率。
33 0
C# | 通过反射将对象属性展示在TreeView中
|
4月前
|
安全 C# 图形学
C#的反射机制
C#的反射机制
|
5月前
|
存储 Java API
C#反射(Reflection)详解
反射是.NET中的重要机制,通过反射可以得到*.exe或*.dll等程序集内部的接口、类、方法、字段、属性、特性等信息,还可以动态创建出类型实例并执行其中的方法。反射指程序可以访问、检测和修改它本身状态或行为的一种能力。通俗一点:我们在获取其他实体类的字段名或实列,只能获取公有的,而有了反射之后可以获取私有的,可以获取他的基类等等,可以说把家底查得清清楚楚。
31 1
|
7月前
|
开发框架 缓存 安全
C#OOP之十三 组件、程序集与反射
C#OOP之十三 组件、程序集与反射
37 0
|
7月前
|
开发框架 .NET Java
C#下反射动态加载dll后如何卸载?
C#下反射动态加载dll后如何卸载?
|
7月前
|
Oracle 架构师 关系型数据库
C#反射应用之实现动态可配置可扩展框架的简单示例
C#反射应用之实现动态可配置可扩展框架的简单示例
|
7月前
|
Java 关系型数据库 MySQL
C#反射(Reflection)详解及于java反射的对比
C#反射(Reflection)详解及于java反射的对比
|
9月前
|
C# 数据安全/隐私保护
C#机房重构-修改密码之职责链模式+反射
C#机房重构-修改密码之职责链模式+反射
42 0
|
10月前
|
运维 Devops C#
【C#编程最佳实践 十七】反射工厂最佳实践
【C#编程最佳实践 十七】反射工厂最佳实践
58 0