性能难题:PropertyInfo属性的读取为什么哪么慢???
下面的程序调用ProcessProperties函数可以返回某个对象的属性与值字串,用来作为对象缓存的关键词,
但是发现这个读属性的过程速度难以接受。用单元测试竟然要15.625毫秒。晕。
究竟如何改进才能提高速度呢?头疼。。。。向各位高手求教了。。。。谢谢。。。。
public class CacheKey
{
private static Type typeCacheKeyAttr = typeof(CacheKeyAttribute);
/// <summary>
/// 处理属性
/// </summary>
/// <param name="baseObject"></param>
/// <returns></returns>
private static string ProcessProperties(object baseObject)
{
// 读取对象属性数组集合
PropertyInfo[] properties = baseObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
System.Collections.Generic.List<string> cachekeys = new System.Collections.Generic.List<string>();
// 遍历属性数组
foreach (PropertyInfo pi in properties)
{
cachekeys.Add(string.Format("{0}:{1}", KeyName(pi), pi.GetValue(baseObject, null)));
}
return string.Join("-", (string[])cachekeys.ToArray());
}
/// <summary>
/// 关键词名称
/// </summary>
/// <param name="mi"></param>
/// <returns></returns>
private static string KeyName(MemberInfo mi)
{
if (mi.IsDefined(typeCacheKeyAttr, true))
return ((CacheKeyAttribute)mi.GetCustomAttributes(typeCacheKeyAttr, true)[0]).KeyName;
else
return mi.Name;
}
}
/// <summary>
/// 缓存关键词特性类(特性类型都直接或间接地从 Attribute 类派生)
/// 注:类引这里的特性类名时可以用全名,也可以前缀名称都可以正常工作;
/// Indicates the property should be part of the cache key if set and other than the default value
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class CacheKeyAttribute : System.Attribute
{
/// <summary>
/// String to use in place of the property or field name in the cache key creation
/// </summary>
/// <param name="keyName"></param>
public CacheKeyAttribute(string keyName)
{
KeyName = keyName;
Ignored = false;
HasDefaultValue = false;
}
/// <summary>
/// String to use in place of the property or field name in the cache key creation Only non default values will be included in the final key
/// </summary>
/// <param name="keyName"></param>
/// <param name="defaultValue"></param>
public CacheKeyAttribute(string keyName, object defaultValue)
{
KeyName = keyName;
DefaultValue = defaultValue;
HasDefaultValue = true;
Ignored = false;
}
/// <summary>
/// Always ingore this field or property in cache key generation if set to true (no need to set this to true if other constructors are used)
/// </summary>
/// <param name="includeInKey"></param>
public CacheKeyAttribute(bool includeInKey)
{
Ignored = !includeInKey;
HasDefaultValue = false;
}
public readonly string KeyName;
public readonly object DefaultValue;
public readonly bool HasDefaultValue;
public readonly bool Ignored;
}
相关推荐
C#中属性PropertyInfo的使用
昨天编程遇到一个问题两个类字段都是二十多个,其中有十多个是相同的,需要将一个类的字段赋值给另外一个类,开始的自己想手动的一个个去赋值,后来想来一下C#基础知识,用PropertyInfo可以解决类似的问题,博客园写博客需要内容详实,我还是慢慢的C#属性从头写起,先看下属性的定义吧: 属性定义:它提供灵活的机制来读取、编写或计算某个私有字段的值。 可以像使用公共数据成员一样使用属性,但实际上它们是称作“访问器”的特殊方法。 这使得可以轻松访问数据,此外还有助于提高方法的安全性和灵活性。属性通常可以分为常规属性和自动属性。两者之间还是有一点区别的,开始编程对着两个全无概念。 常规属性
property-info:PropertyInfo使用流行资源的元数据提取有关PHP类的属性的信息
PropertyInfo组件 PropertyInfo组件使用流行资源的元数据提取有关PHP类的属性的信息。 资源资源 并在
C#通过属性名称获取(读取)属性值的方法
本文主要介绍了C#通过属性名称获取(读取)属性值的方法,并提供了简化版代码,具有很好的参考价值,需要的朋友可以看下
C#中使用反射遍历一个对象属性及值的小技巧
主要介绍了C#中使用反射遍历一个对象属性及值的小技巧,这在很时候应该都非常有用,本文直接给出实例代码,需要的朋友可以参考下
C#利用反射来判断对象是否包含某个属性的实现方法
本文实例展示了C#利用反射来判断对象是否包含某个属性的实现方法,对于C#程序设计人员来说有一定的学习借鉴价值。 具体实现代码如下: /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> /// <param name=instance>object</param> /// <param name=propertyName>需要判断的属性</param> /// <returns>是否包含</returns> public static bool ContainProperty(this object instance, string p


