简体   繁体   中英

Return reference not copy with GetValue()

using reflection in c# I need to get only reference to object, not copy, is it possible?

object data = actualData.GetType().GetProperty(properties[0]).GetValue(actualData, null);

variable data should be only reference - if I change something inside, I would like to perform the same changes in actualData variable, but it seems to copy value from actual data and any change stay only in data variable. Any suggestion? Thanks!

It seems that your property is of value type. Or maybe it is of reference type but it creates a new instance of resulting class instead of reusing it.

If this is a case, then I'm afraid this can't be done in general - properties are methods, so the result value of property is calculated, you cannot observe when that result would be changed.

But if you can change class of which actualData is the instance, then you can implement INotifyPropertyChanged interface and subscribe to its event PropertyChanged in your code.

If this is not possible then you can remember the result of ...GetProperty(), but you have to call GetValue() on it each time when you need the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM