Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我能够找到的FieldInfo文档没有比较和对比这两种方法。我有兴趣了解什么时候应该使用另一个(最好使用一个小代码片段)。
FieldInfo
SetValueDirect 采用 TypedReference 参数,因此在需要 varargs 参数互操作性时使用。这不会对值进行装箱/拆箱,也不会进行访问检查。它比 SetValue 运行得更快。
另一方面,SetValue 几乎适用于其他所有内容。它在需要时执行参数的装箱/拆箱,并检查访问。
有一些 SO在这里和这里讨论 TypedReferences - 两者都有代码示例。