.NET我想克隆值类型的字段。我如何使用反射(或其他动态)在值类型上设置字段值?.NET如何设置字段值的值类型使用反射
这适用于引用类型,但不适用于值类型。我明白为什么,但我不知道替代方案。
shared function clone(of t)(original as t) as t
dim cloned as t
'if class then execute parameterless constructor
if getType(t).isClass then cloned = reflector.construct(of t)()
dim public_fields = original.getType.getFields()
for each field in public_fields
dim original_value = field.getValue(original)
'this won't work for value type, but it does work for reference type ???
field.setValue(cloned, original_value)
next
return cloned
end function
几乎你代码中的每一个单词都是大写字母。 – SLaks 2010-03-23 00:12:48
哈哈,不知何故我喜欢它 – soccerazy 2010-03-23 04:01:23