考虑将消息定义为特殊类型,每个消息都有自己的属性集和总长度,我需要选择以下解决方案之一:
1.制作使用泛型的方法,例如Encode< T>(…),其中T:IMessage然后为每种类型的消息实现编码器/解码器并且具有ResolveEncoder< T>.那将为想要的消息选择编码器
要么
2.创建使用任何类型的消息的方法,只要它实现IMessage,例如Encode(IMessage消息),并使用System.Reflection来确定我需要了解的所有内容.
我对解决方案#2更感兴趣,因为它使我的代码缩小了30倍.但是,我担心不断反映属性是否会影响性能.现在由于时间限制完成项目,我无法真正“实验”.
我将非常感谢任何个人经验或与基准测试相关的性能与任何一种解决方案的关系.
解决方法 反思可以足够快.但需要正确实施.反思表现 –
快速和轻便的功能
typeofObject.GetTypetypeof == Object.GetTypeType equivalence APIs (including typehandle operator overloads)get_Moduleget_MemberTypeSome of the IsXX predicate APIsNew token/handle resolution APIs in the .NET Framework 2.0
昂贵的功能
GetXX APIs (MethodInfo,PropertyInfo,FIEldInfo,and so on)GetCustomAttributesType.InvokeMemberInvoke APIs (MethodInfo.Invoke,FIEldInfo.GetValue,and so on)get_name (name property)Activator.CreateInstance
Source – Dodge Common Performance Pitfalls to Craft Speedy Applications
MethodInfo可以加速 – Improving performance reflection,what alternatives should I consider
好的链接:
How costly is .NET reflection?
http://www.codeproject.com/Articles/18450/HyperDescriptor-Accelerated-dynamic-property-acces
以上是内存溢出为你收集整理的c# – System.Reflection vs Generics – 性能全部内容,希望文章能够帮你解决c# – System.Reflection vs Generics – 性能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)