Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 NDepende CQL报告方法不正确地使用装箱/拆箱_C# 4.0_Generics_Boxing_Ndepend - Fatal编程技术网

C# 4.0 NDepende CQL报告方法不正确地使用装箱/拆箱

C# 4.0 NDepende CQL报告方法不正确地使用装箱/拆箱,c#-4.0,generics,boxing,ndepend,C# 4.0,Generics,Boxing,Ndepend,在NDepend4(v4.1.0.6871)中,我使用默认设计查询“应避免装箱/拆箱”: 它报告了以下使用拳击的方法(受Jon Skeet的启发,并从他那里偷来): public static void ThrowIfNull<T>(this T target, string name) where T : class { if (target == null) { throw new ArgumentNullException(name ?? string.Empt

在NDepend4(v4.1.0.6871)中,我使用默认设计查询“应避免装箱/拆箱”:

它报告了以下使用拳击的方法(受Jon Skeet的启发,并从他那里偷来):

public static void ThrowIfNull<T>(this T target, string name) where T : class
{
  if (target == null)
  {
    throw new ArgumentNullException(name ?? string.Empty);
  }
}
publicstaticvoidthrowifnull(这个T目标,字符串名),其中T:class
{
if(target==null)
{
抛出新ArgumentNullException(名称??string.Empty);
}
}
我不明白这个方法怎么可能使用拳击。我没有在任何地方使用石膏

我尝试了以下版本,以防空合并操作符在幕后使用装箱:

public static void ThrowIfNull<T>(this T target, string name) where T : class
{
  if (target == null)
  {
    throw new ArgumentNullException(name);
  }
}
publicstaticvoidthrowifnull(这个T目标,字符串名),其中T:class
{
if(target==null)
{
抛出新的ArgumentNullException(名称);
}
}
。。。但我也没有运气,NDepend仍然报告说这种方法是使用拳击


有什么想法吗?

通过使用.NET Reflector反编译此方法,我们可以看到此方法确实使用了
box
IL指令。尽管您使用的是
泛型约束,编译器仍然会发出一条box指令来解决可验证性问题。对此有更多的解释

.method public hidebysing static void ThrowIfNull(!!T target,string name)cil managed
{
.custom instance void[mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
.maxstack 2
.init(
[0]布尔标志)
L_0000:没有
L_0001:ldarg.0

L_0002:box!!T通过使用.NET Reflector反编译此方法,我们可以看到此方法确实使用了
box
IL指令。尽管您使用的是
泛型约束,编译器仍然会发出一条box指令以解决可验证性问题。有关此问题的详细说明

.method public hidebysing static void ThrowIfNull(!!T target,string name)cil managed
{
.custom instance void[mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
.maxstack 2
.init(
[0]布尔标志)
L_0000:没有
L_0001:ldarg.0
L_0002:box!!T
public static void ThrowIfNull<T>(this T target, string name) where T : class
{
  if (target == null)
  {
    throw new ArgumentNullException(name);
  }
}
.method public hidebysig static void ThrowIfNull<class T>(!!T target, string name) cil managed
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
    .maxstack 2
    .locals init (
        [0] bool flag)
    L_0000: nop 
    L_0001: ldarg.0 
    L_0002: box !!T   <-----------
    L_0007: ldnull 
    L_0008: ceq 
    L_000a: ldc.i4.0 
    L_000b: ceq 
    L_000d: stloc.0 
    L_000e: ldloc.0 
    L_000f: brtrue.s L_0022
    L_0011: nop 
    L_0012: ldarg.1 
    L_0013: dup 
    L_0014: brtrue.s L_001c
    L_0016: pop 
    L_0017: ldsfld string [mscorlib]System.String::Empty
    L_001c: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string)
    L_0021: throw 
    L_0022: ret 
}