性能难题:PropertyInfo属性的读取为什么哪么慢???

aploo 2008-07-08 10:11:23
性能难题: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;
}
...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuadam 2008-07-08
  • 打赏
  • 举报
回复
用Dictionary存PropertyInfo不就行了,还用cache多麻烦
aploo 2008-07-08
  • 打赏
  • 举报
回复
如果把这行注释掉: cachekeys.Add(string.Format("{0}:{1}", KeyName(pi), pi.GetValue(baseObject, null)));

时间就降为0, 说明性能就消耗在这行语句上....
aploo 2008-07-08
  • 打赏
  • 举报
回复
先谢了...
aploo 2008-07-08
  • 打赏
  • 举报
回复
不是文件, 只是一个查询用的类对象而已....
yilanwuyu123 2008-07-08
  • 打赏
  • 举报
回复
这应该是缓存的问题吧 FOREACH每一次执行都会打开这个文件
aploo 2008-07-08
  • 打赏
  • 举报
回复
upp

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧