C#方法似乎没有运行

Ian*_*ton 2 c# unity-container

ReflectionHelperMicrosoft.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

attributesAsObjects包含一个元素.attributes包含一个元素,但它是null.ForEach块(我已经扩展)似乎没有运行 - 没有命中断点.

这巫术是什么?

调用代码,这导致在一个空引用异常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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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();
}
Run Code Online (Sandbox Code Playgroud)

nvo*_*igt 5

这巫术是什么?

它被称为"延迟执行",基本上意味着如果你实际上没有枚举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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这种方法很危险.它在你身边.它的使用方式支持我的理论:错误.

new[]{ 6, 7 }.ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)

这条线做什么?什么都没有!为什么?因为您需要实现结果才能实际执行任何代码:

new[]{ 6, 7 }.ForEach(Console.WriteLine).ToList();
Run Code Online (Sandbox Code Playgroud)

将打印67.

同样,此代码段:

attributesAsObjects.ForEach(attr =>
{
    var a = (TAttribute) attr;

    attributes[index++] = a;
});
Run Code Online (Sandbox Code Playgroud)

什么.由于结果未实现,因此不会执行代码.

  • 你的`ForEach`方法没有被执行,因为它的结果从未使用过.你需要实现它的结果(例如用`.ToList()`调用它来*实际运行*. (2认同)