.NET How can i set field value on value type using reflection
Posted
by soccerazy
on Stack Overflow
See other posts from Stack Overflow
or by soccerazy
Published on 2010-03-23T00:09:00Z
Indexed on
2010/03/23
0:11 UTC
Read the original article
Hit count: 679
.NET
|reflection
.NET I want to clone a value type's fields. How can i set a field value on a value type using reflection (or something else dynamically)?
This works for reference types but not for value types. I understand why but I don't know an alternative.
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
© Stack Overflow or respective owner