开发者

using reflection for this, is there another way?

I ha开发者_运维知识库ve a list of keyvaluepair(string,string) first string is something like class.property, second string is the value to assign to that class.property.

I'm currently looping through that list and by using reflection I set every value.

it work, but my question is there a faster way to do this?


If you're using the same property repeatedly (e.g. against a variety of target objects), it's faster to build a delegate out of the setter using Delegate.CreateDelegate and use that. However, I'd only do that when I'd profiled it and found that this was a bottleneck. It happened to make a big difference against my protocol buffer implementation, but that really needs to be as fast as possible, so I'm happy to apply a bit of micro-optimisation.


If you need to set the same property, or properties on the same type, more than once, cache your Type and PropertyInfo's. It is very easy stuff to cache - and examining the types is what typically takes the time in reflection scenarios.

If you can use C# 4 for this, you might be able to come up with a faster solution using dynamic. (I have seen the .NET 3.5 tag, just thought I would mention it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜