C# Reflection: What is the difference between FieldInfo.SetValue() and FieldInfo.SetValueDirect()?
Asked Answered
S

1

7

The FieldInfo documentation I was able to find did not compare and contrast these two methods. I am interested in understanding when one should be used over the other (preferably with a small code snippet).

Shirashirah answered 26/5, 2014 at 2:24 Comment(2)
So do you have a TypedReference you can use for SetValueDirect?Slipknot
Does SetValueDirect() run faster than SetValue()?Vita
I
10

SetValueDirect takes a TypedReference parameter, and as such it is to be used when varargs parameter interoperability is needed. This performs no boxing/unboxing of values and no access checking. It runs faster than SetValue.

SetValue on the other hand operates on pretty much everything else. It performs boxing/unboxing of the parameter where needed, and also checks for access.

There are SO's that discuss TypedReferences here and here - there are code examples in both.

Incalculable answered 26/5, 2014 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.