private static Dictionary<Type, MethodInfo> s_GetItemMethodInfos = new Dictionary<Type, MethodInfo>();
public static bool TryGet<T>(this SPBaseCollection spBase, string name, out T result)
{
result = default(T);
try
MethodInfo methodInfo = null;
Type spCollectionType = spBase.GetType();
if (s_GetItemMethodInfos.ContainsKey(spCollectionType) == false)
methodInfo = spCollectionType.GetMethod("get_Item",
BindingFlags.InvokeMethod | BindingFlags.ExactBinding | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.DeclaredOnly,
null,
new Type[] { typeof(string) },
null);
s_GetItemMethodInfos.Add(spCollectionType, methodInfo);
}
else
methodInfo = s_GetItemMethodInfos[spCollectionType];
if (methodInfo == null)
return false;
result = (T)methodInfo.Invoke(spBase, new object[] { name });
return true;
catch (Exception ex)
Trace.Write(String.Format("Extension Method TryGet : {0}", ex.InnerException.Message), "Monitorable");
dynamic dynamicSpBase = spBase;
result = dynamicSpBase.get_Item(name);
result = (T)dynamicSpBase[name];
object dynamicSpBase = spBase;
if (<TryGet>o__SiteContainer0<T>.<>p__Site1 == null)
<TryGet>o__SiteContainer0<T>.<>p__Site1 = CallSite<Func<CallSite, object, T>>.Create(
new CSharpConversionPayload(Microsoft.CSharp.RuntimeBinder.RuntimeBinder.GetInstance(),
typeof(T),
CSharpConversionPayload.ConversionKindEnum.ImplicitConversion));
if (<TryGet>o__SiteContainer0<T>.<>p__Site2 == null)
<TryGet>o__SiteContainer0<T>.<>p__Site2 = CallSite<Func<CallSite, object, string, object>>.Create(
new CSharpCallPayload(Microsoft.CSharp.RuntimeBinder.RuntimeBinder.GetInstance(),
false,
"get_Item",
typeof(object),
null));
result = <TryGet>o__SiteContainer0<T>.<>p__Site1.Target(
<TryGet>o__SiteContainer0<T>.<>p__Site1,
<TryGet>o__SiteContainer0<T>.<>p__Site2.Target(
<TryGet>o__SiteContainer0<T>.<>p__Site2,
dynamicSpBase,
name));
Trace.Write(string.Format("Extension Method TryGet : {0}", ex.InnerException.Message), "Monitorable");