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.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)
相關文章
- web中,利用反射給物件賦值Web反射物件賦值
- C#反射設定屬性值和獲取屬性值C#反射
- C# 類相同屬性賦值C#賦值
- 物件屬性值賦給變數物件變數
- final屬性值能被反射修改嗎?反射
- Java反射給泛型集合賦值Java反射泛型賦值
- TypeScript 介面 只讀屬性TypeScript
- C#給自動屬性設定預設值C#
- Javascript 解構賦值,將屬性/值從物件/陣列中取出,賦值給其他變數JavaScript賦值物件陣列變數
- Java反射-屬性Java反射
- 使用反射為特性賦值反射賦值
- Java 反射修改類的常量值、靜態變數值、屬性值Java反射變數
- 【spring原始碼系列】之【Bean的屬性賦值】Spring原始碼Bean賦值
- [非專業翻譯] Mapster - 對映只讀屬性
- C#獲取某個物件的屬性值C#物件
- C# 隨機給一個全部資訊都未知的類型別,如何獲取該類的類名、屬性個數、屬性名、屬性的資料型別、屬性值?C#隨機資料型別
- js 利用||和&&賦值小技巧JS賦值
- dotnet C# 給結構體欄位賦值非執行緒安全C#結構體賦值執行緒
- vue:動態給img賦值Vue賦值
- Mysql的read_only 只讀屬性說明 (運維筆記)MySql運維筆記
- 根據屬性字串獲取屬性值字串
- C#屬性和lamdaC#
- vue3 父元件【屬性】傳值給子元件【props】接收Vue元件
- 對Mysql中的read_only 只讀屬性做簡要說明MySql
- 利用反射讀取資料庫資料反射資料庫
- c#小灶——常量、變數和賦值C#變數賦值
- C#中屬性的解析C#
- C#屬性與欄位C#
- 瞭解下C# 屬性(Property)C#
- HTML 布林屬性值HTML
- 修改追加屬性的值
- 揭秘Java反射:如何輕鬆獲取類的屬性及父類屬性Java反射
- JAVA之反射學習3-反射獲取成員變數並賦值Java反射變數賦值
- SQL Server 生成C#公共實體屬性和私有屬性SQLServerC#
- 給numpy陣列賦同樣的值陣列
- WPF當屬性值改變時利用PropertyChanged事件來載入動畫事件動畫
- 給Product新增自定義屬性
- 針對限定作者、書名均為只讀屬性的程式碼及其疑問
- [JAVA] 只知物件屬性,不知類屬性?就算類答應,static都不答應Java物件