gpt4 book ai didi

C# 方法似乎没有运行

转载 作者:行者123 更新时间:2023-12-04 00:24:23 25 4
gpt4 key购买 nike

来自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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com