


Can C# use reflection to assign values to read-only properties?
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 note:
TypeDescriptor.GetProperties is used to setvalue, which has no effect:
TypeDescriptor.GetProperties(rt)["ID"].SetValue(rt, "Guid");
So why does TypeDescriptor.GetProperties use Why does setvalue have no effect?
Split the above code into the following two sentences:
##PropertyDescriptor prop = TypeDescriptor.GetProperties(rt)["ID"];
prop.SetValue(rt, "Guid");
#After obtaining the instance of the abstract class PropertyDescriptor, in When the SetValue method is called, it is called from its subclass ReflectPropertyDescriptor.
## And be specific The implementation is in the subclass: ReflectPropertyDescriptor. Find ReflectPropertyDescriptor and SetValue from Microsoft source code
##
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); } } } } }
It can be seen from the code, The read-only attribute is skipped directly. . . . . .
So are there any restrictions on PropertyInfo?
The SetValue called by PropertyInfo is as follows:
The specific implementation can be found in Microsoft's open source code 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 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); }
I have not seen the
PropertyInfo call for the time being. What are the limitations of #SetValue
##PropertyInfo.GetSetMethod method (Boolean)
The above is C#. Can you use reflection to assign values to read-only properties? For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.
