Is there any. I just ran the new perf an开发者_如何学编程alysis tool of vs2010 and as it turns out this func consumes most of the cpu resources.
Is there something else i can use?
You don't give enough details about your situation to provide a definitive answer, but if you are repeatedly setting the same field (but you don't know which field until runtime) then you can create a dynamic method to set the field, and repeatedly call that instead. See the DynamicMethod class for more info (how to article, case study with examples).
You may be able to achieve the same thing more easily by building an expression to set the field (using the System.Linq.Expressions classes) and compiling them using LambdaExpression.Compile. This is generally easier than using Reflection.Emit, but may not work for you because fields are typically private members.
Note that if you are setting lots of different fields, and only setting each field a small number of times, then the overhead of generating dynamic methods or compiling expressions is far greater than calling FieldInfo.SetValue. As always, measure before making a decision!
Unless you belong to the select few who enjoy spending time figuring out IL to get their work done then I believe you will be better served by picking a library to do this for you.
I can recommend Fasterflect, which was just released in v2.0 and supports both .NET 3.5 and 4.0. It uses light-weight code generation and caching for a 4-5x speed boost over regular reflection, or direct access to the generated delegates for almost native speeds (not counting the considerable overhead for the initial JIT-compiling of the dynamic method).
Disclaimer: I am involved in said project as a contributor.
精彩评论