gpt4 book ai didi

c# - 使用反射获取属性引用

转载 作者:太空狗 更新时间:2023-10-29 22:59:30 25 4
gpt4 key购买 nike

var a = new obj();
var property = a.GetType().GetProperty("DB").GetValue(a,null) as testObject;

这是否意味着变量 property 持有对我在对象 a 中获得的同一对象的引用,或者创建了一个持有相同值的新 testObject?

如果这意味着创建一个新对象,那么我如何使用反射获取对该属性/支持字段的引用?

最佳答案

property 现在拥有 refereceaDB 属性中的任何内容。

虽然我不确定当您在具有值类型的属性上调用 GetValue() 时会发生什么,但我想您得到了对原始值的盒装副本的引用,如前所述在 Boxing and Unboxing (C# Programming Guide) :

Boxing a value type [to object in GetValue()'s case] allocates an object instance on the heap and copies the value into the new object.

关于c# - 使用反射获取属性引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20871744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com