[英]Getting a property reference using reflection
var a = new obj();
var property = a.GetType().GetProperty("DB").GetValue(a,null) as testObject;
does this mean that the variable property
hold a reference to the the same object that i got in object a , or a new testObject was made that holds the same values? 这是否意味着变量
property
持有对对象a中相同对象的引用,或者是否创建了具有相同值的新testObject?
if this means creating a new object, then how can i get the reference to that property/backing field using reflection? 如果这意味着创建一个新对象,那么如何使用反射获得对该属性/支持字段的引用?
property
now holds a referece to whatever is in a
's DB
property. property
现在持有referece到无论是在a
的DB
属性。
I'm not sure though what happens when you call GetValue()
on a property that has a value type, I suppose you get a reference to a boxed copy of the original value, as explained in Boxing and Unboxing (C# Programming Guide) : 我不确定当你在具有值类型的属性上调用
GetValue()
时会发生什么,我想你会得到对原始值的盒装副本的引用,如Boxing和Unboxing(C#编程指南)中所述 :
Boxing a value type [to
object
inGetValue()
's case] allocates an object instance on the heap and copies the value into the new object.装箱值类型[到
object
中GetValue()
的情况下]分配在堆上并复制的对象实例的值到新的对象。
这意味着一个属性的值DB
你的对象的a
投,以testType
(你传递一个参考对象a
在PropertyInfo.GetValue
),所以只有一个对象a
和你的变量引用它的DB
性能。
Variable property
holds reference to same value stored in property DB
. 变量
property
保存对属性DB
存储的相同值的引用。
GetValue(a,null)
return type is object so with as operator you are simple type casting it. GetValue(a,null)
返回类型是对象,因此使用as运算符可以简单地类型转换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.