如何查看c#自带方法的源代码
其他问答
1
比如list.Reverse()(将list中的元素反转),我怎么能看到它具体是如何实现的?
-
public void Reverse(int index, int count) { if (index < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (_size - index < count) ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); Contract.EndContractBlock(); Array.Reverse(_items, index, count); _version++; }
内部调用的是array的Array.Reverse(_items, index, count);
https://referencesource.microsoft.com/#mscorlib/system/array.cs,f5edebc3412b666b
public static void Reverse(Array array, int index, int length) { if (array==null) throw new ArgumentNullException("array"); if (index < array.GetLowerBound(0) || length < 0) throw new ArgumentOutOfRangeException((index<0 ? "index" : "length"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (array.Length - (index - array.GetLowerBound(0)) < length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); if (array.Rank != 1) throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); Contract.EndContractBlock(); bool r = TrySZReverse(array, index, length); if (r) return; int i = index; int j = index + length - 1; Object[] objArray = array as Object[]; if (objArray!=null) { while (i < j) { Object temp = objArray[i]; objArray[i] = objArray[j]; objArray[j] = temp; i++; j--; } } else { while (i < j) { Object temp = array.GetValue(i); array.SetValue(array.GetValue(j), i); array.SetValue(temp, j); i++; j--; } } }
-
使用的dll不是自己源码生成的是跳转不到对应的方法中,需要把源程序代码导入到自己的工程中才能查看对应的方法。
发表回复