C# 方法似乎没有 运行
C# method doesn't appear to run
从 ReflectionHelper
到 Microsoft.Practices.Unity.InterceptionExtension
...
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods",
Justification = "Validation done by Guard class")]
public static TAttribute[] GetAttributes<TAttribute>(MemberInfo member, bool inherits) where TAttribute : Attribute
{
Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(member, "member");
IEnumerable<Object> attributesAsObjects = member.GetCustomAttributes(typeof(TAttribute), inherits);
TAttribute[] attributes = new TAttribute[attributesAsObjects.Count()];
int index = 0;
attributesAsObjects.ForEach(attr =>
{
var a = (TAttribute) attr;
attributes[index++] = a;
});
return attributes;
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
yield return item;
}
}
attributesAsObjects
包含一个元素。 attributes
包含一个元素,但它是 null
。 ForEach 块(我已经展开)似乎 运行 - 断点未命中。
这是什么巫术?
calling code,导致 attr
上的空引用异常......
protected override IEnumerable<ICallHandler> DoGetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
{
if (member.InterfaceMethodInfo != null)
{
foreach (HandlerAttribute attr in ReflectionHelper.GetAllAttributes<HandlerAttribute>(member.InterfaceMethodInfo, true))
{
yield return attr.CreateHandler(container);
}
}
foreach (HandlerAttribute attr in ReflectionHelper.GetAllAttributes<HandlerAttribute>(member.ImplementationMethodInfo, true))
{
yield return attr.CreateHandler(container);
}
}
这里是 GetAllAttributes
...(注意最后对 ToArray
的调用。)
public static TAttribute[] GetAllAttributes<TAttribute>(MemberInfo member, bool inherits)
where TAttribute : Attribute
{
Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(member, "member");
List<TAttribute> attributes = new List<TAttribute>();
if (member.DeclaringType != null)
{
attributes.AddRange(GetAttributes<TAttribute>(member.DeclaringType.GetTypeInfo(), inherits));
MethodInfo methodInfo = member as MethodInfo;
if (methodInfo != null)
{
PropertyInfo prop = GetPropertyFromMethod(methodInfo);
if (prop != null)
{
attributes.AddRange(GetAttributes<TAttribute>(prop, inherits));
}
}
}
attributes.AddRange(GetAttributes<TAttribute>(member, inherits));
return attributes.ToArray();
}
What is this witchcraft?
它被称为"deferred execution",基本上意味着如果你没有实际枚举一个IEnumerable<T>
,它就不会被执行。
详细说明:
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
yield return item;
}
}
此方法危险。它骗你。它的使用方式支持我的理论:错误。
new[]{ 6, 7 }.ForEach(Console.WriteLine);
这条线是做什么的? 没有!为什么?因为您需要实现结果才能实际执行任何代码:
new[]{ 6, 7 }.ForEach(Console.WriteLine).ToList();
这 将打印 6
和 7
.
同样,这个代码片段:
attributesAsObjects.ForEach(attr =>
{
var a = (TAttribute) attr;
attributes[index++] = a;
});
什么都不做。因为结果没有具体化,所以代码根本就没有执行。
从 ReflectionHelper
到 Microsoft.Practices.Unity.InterceptionExtension
...
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods",
Justification = "Validation done by Guard class")]
public static TAttribute[] GetAttributes<TAttribute>(MemberInfo member, bool inherits) where TAttribute : Attribute
{
Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(member, "member");
IEnumerable<Object> attributesAsObjects = member.GetCustomAttributes(typeof(TAttribute), inherits);
TAttribute[] attributes = new TAttribute[attributesAsObjects.Count()];
int index = 0;
attributesAsObjects.ForEach(attr =>
{
var a = (TAttribute) attr;
attributes[index++] = a;
});
return attributes;
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
yield return item;
}
}
attributesAsObjects
包含一个元素。 attributes
包含一个元素,但它是 null
。 ForEach 块(我已经展开)似乎 运行 - 断点未命中。
这是什么巫术?
calling code,导致 attr
上的空引用异常......
protected override IEnumerable<ICallHandler> DoGetHandlersFor(MethodImplementationInfo member, IUnityContainer container)
{
if (member.InterfaceMethodInfo != null)
{
foreach (HandlerAttribute attr in ReflectionHelper.GetAllAttributes<HandlerAttribute>(member.InterfaceMethodInfo, true))
{
yield return attr.CreateHandler(container);
}
}
foreach (HandlerAttribute attr in ReflectionHelper.GetAllAttributes<HandlerAttribute>(member.ImplementationMethodInfo, true))
{
yield return attr.CreateHandler(container);
}
}
这里是 GetAllAttributes
...(注意最后对 ToArray
的调用。)
public static TAttribute[] GetAllAttributes<TAttribute>(MemberInfo member, bool inherits)
where TAttribute : Attribute
{
Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(member, "member");
List<TAttribute> attributes = new List<TAttribute>();
if (member.DeclaringType != null)
{
attributes.AddRange(GetAttributes<TAttribute>(member.DeclaringType.GetTypeInfo(), inherits));
MethodInfo methodInfo = member as MethodInfo;
if (methodInfo != null)
{
PropertyInfo prop = GetPropertyFromMethod(methodInfo);
if (prop != null)
{
attributes.AddRange(GetAttributes<TAttribute>(prop, inherits));
}
}
}
attributes.AddRange(GetAttributes<TAttribute>(member, inherits));
return attributes.ToArray();
}
What is this witchcraft?
它被称为"deferred execution",基本上意味着如果你没有实际枚举一个IEnumerable<T>
,它就不会被执行。
详细说明:
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
foreach (T item in enumeration)
{
action(item);
yield return item;
}
}
此方法危险。它骗你。它的使用方式支持我的理论:错误。
new[]{ 6, 7 }.ForEach(Console.WriteLine);
这条线是做什么的? 没有!为什么?因为您需要实现结果才能实际执行任何代码:
new[]{ 6, 7 }.ForEach(Console.WriteLine).ToList();
这 将打印 6
和 7
.
同样,这个代码片段:
attributesAsObjects.ForEach(attr =>
{
var a = (TAttribute) attr;
attributes[index++] = a;
});
什么都不做。因为结果没有具体化,所以代码根本就没有执行。