简体   繁体   English

Convert.ChangeType() 是否只是返回对传递值的不同类型的引用?

[英]Does Convert.ChangeType() just return a differently typed reference to the passed value?

The Microsoft docs say that Convert.ChangeType returns Microsoft 文档说 Convert.ChangeType 返回

An object whose type is conversionType and whose value is equivalent to value.类型为conversionType 且值等价于value 的对象。

I'm not sure what that means.我不确定这意味着什么。 Here's my scenario.这是我的场景。

ParentType parent = new ChildType();

Type type = parent.GetType();

parent.SomeMemberProperty = 1;

dynamic child = Convert.ChangeType(parent, type);

child.SomeMemberProperty = 2;

What then is the result of parent.SomeMemberProperty ?那么parent.SomeMemberProperty的结果是parent.SomeMemberProperty

EDIT: Please ignore the simplicity/stupidity of the above scenario.编辑:请忽略上述场景的简单性/愚蠢性。 It's just meant to demonstrate the confusion I have and I'd never actually write that code.这只是为了证明我的困惑,我从来没有真正写过那些代码。

Convert.ChangeType() does not copy the parent. Convert.ChangeType()不复制父级。 It merely provides a differently typed reference to it, so any mutations to child will affect the parent object as well.它仅提供对其的不同类型的引用,因此对 child 的任何更改也会影响父对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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