繁体   English   中英

C#方法似乎未运行

[英]C# method doesn't appear to run

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块(已扩展)似乎没有运行-没有命中断点。

这是什么巫术?

调用代码 ,这会导致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();
}

这是什么巫术?

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

什么没做 由于未实现结果,因此根本不会执行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM