这段时间在学习人家的开源代码,有个类不太明白作者的意图,现在贴上来,请教一下大家!
using System;
using System.Collections.Generic;
using System.Reflection;using Phydeaux.Utilities;namespace NCrawler.Utils
{
internal class ReflectionCache
{
#region Readonly & Static Fields private static Dictionary<string, Phydeaux.Utilities.Func<object, object>> s_GetterDelegateCache =
new Dictionary<string, Phydeaux.Utilities.Func<object, object>>(); private static Dictionary<string, Proc<object, object>> s_SetterDelegateCache =
new Dictionary<string, Proc<object, object>>(); #endregion #region Class Methods public static Phydeaux.Utilities.Func<object, object> LookupGetterDelegate(Type type, string propertyName)
{
Phydeaux.Utilities.Func<object, object> getterDelegate; string key = type.GetType() + "_" + propertyName; // Check if in cache
if (s_GetterDelegateCache.TryGetValue(key, out getterDelegate))
{
return getterDelegate;
} PropertyInfo pi = type.GetProperty(propertyName);
if(pi != null)
{
getterDelegate =
Dynamic<object>.DynamicBase<Phydeaux.Utilities.Func<object, object>>.Explicit.Instance.Property<object>.Getter.
CreateDelegate(pi);
} // Copy Dictionary
lock (s_GetterDelegateCache)
{
// Check if in cache
Phydeaux.Utilities.Func<object, object> getterDelegateTmp;
if (s_GetterDelegateCache.TryGetValue(key, out getterDelegateTmp))
{
return getterDelegateTmp;
} Dictionary<string, Phydeaux.Utilities.Func<object, object>> indexFieldCacheCopy =
new Dictionary<string, Phydeaux.Utilities.Func<object, object>>(s_GetterDelegateCache.Count + 1);
foreach (var item in s_GetterDelegateCache)
{
indexFieldCacheCopy.Add(item.Key, item.Value);
}
indexFieldCacheCopy.Add(key, getterDelegate);
s_GetterDelegateCache = indexFieldCacheCopy;
}
return getterDelegate;
} public static Proc<object, object> LookupSetterDelegate(Type type, string propertyName)
{
Proc<object, object> setterDelegate; string key = type.GetType() + propertyName; // Check if in cache
if (s_SetterDelegateCache.TryGetValue(key, out setterDelegate))
{
return setterDelegate;
} PropertyInfo pi = type.GetProperty(propertyName);
if (pi != null)
{
setterDelegate =
Dynamic<object>.DynamicBase<Proc<object, object>>.Explicit.Instance.Property<object>.Setter.CreateDelegate(
pi);
} // Copy Dictionary
lock (s_SetterDelegateCache)
{
// Check if in cache
Proc<object, object> setterDelegateTmp;
if (s_SetterDelegateCache.TryGetValue(key, out setterDelegateTmp))
{
return setterDelegateTmp;
} Dictionary<string, Proc<object, object>> indexFieldCacheCopy =
new Dictionary<string, Proc<object, object>>(s_SetterDelegateCache.Count + 1);
foreach (var item in s_SetterDelegateCache)
{
indexFieldCacheCopy.Add(item.Key, item.Value);
}
indexFieldCacheCopy.Add(key, setterDelegate);
s_SetterDelegateCache = indexFieldCacheCopy;
}
return setterDelegate;
} #endregion
}
}

解决方案 »

  1.   

    简而言之,就是利用反射机制取得对象的getter跟setter属性
    至于Dynamic<object>.DynamicBase<Proc<object, object>>.Explicit.Instance.Property<object>.Setter.CreateDelegate(
    pi);
    跟Phydeaux.Utilities.Func<object, object>
    没贴上代码鬼知道它要做啥
    s_GetterDelegateCache,s开头是静态变量
    Cache很明显,要将数据暂存
    getter跟setter都一样,如果在cache中已存在就返回getterDelegateTmp或setterDelegateTmp
    若不存在则创建新的setterDelegate或getterDelegate加入Cache池中并返回值
      

  2.   

    // Copy Dictionary上面有注释哦。