C# 方法似乎没有运行

标签 c# unity-container

来自Microsoft.Practices.Unity.InterceptionExtension中的ReflectionHelper...

[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 block (我已展开)似乎没有运行 - 没有命中断点。

这是什么巫术?

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?

这被称为“延迟执行”,基本上意味着如果您没有实际枚举 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();

将打印 67 .

同样,此代码片段:

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

    attributes[index++] = a;
});

什么都不做。因为结果没有具体化,所以代码根本就没有执行。

关于C# 方法似乎没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749546/

相关文章:

c# - 使用 Microsoft Enterprise Library Unity 拦截方法时未返回完整堆栈跟踪

c# - 如何彻底解决IoC循环引用?

unity-container - 如何将 Unity.RegisterType 与 Moq 一起使用?

c# - 结束任务以防止失控的最佳方式是什么

c# - NUnit测试调试

c# - .NET Core SSL - 模板仅在浏览器中显示 PR_CONNECT_RESET_ERROR (Firefox)

c# - 如何调试我创建的 NuGet 包中的代码

c# - 使用 Roslyn SDK 生成器生成的自定义 SonarQube 规则始终发出类型 "Code Smell"

asp.net-mvc - 我应该如何为 Unity 和 AutoMapper 实现 MVC Bootstrapper?

mvvm - 在 Prism 应用程序的上下文中 Unity 与 MEF?