Ejemplo n.º 1
0
        private static bool IsMethodUsed(
            MethodInfo method,
            MethodInfo[] usedSourceMethods,
            DebugLimitData?debugLimitData)
        {
            // ReSharper disable once InvertIf
            if (debugLimitData != null && debugLimitData.HasClassNames)
            {
                var notFound = debugLimitData.ClassMethodNames
                               .Where(x => x.Item2 != null)
                               .All(x => !x.Item2.Any(m => method.BeautifyName().Equals(m, StringComparison.Ordinal)));

                if (notFound)
                {
                    return(true);
                }
            }

            var isEqual = usedSourceMethods.Any(x =>
                                                x.DeclaringType !.GetNameWithoutGenericType(true) !.Equals(method.DeclaringType !.GetNameWithoutGenericType(true), StringComparison.Ordinal) &&
                                                x.BeautifyName().Equals(method.BeautifyName(), StringComparison.Ordinal));

            if (isEqual)
            {
                return(true);
            }

            var methodParameters          = method.GetParameters();
            var usedMethodsByDeclaredType = usedSourceMethods
                                            .Where(x => string.Equals(x.DeclaringType !.BeautifyName(false, false, true), method.DeclaringType !.BeautifyName(false, false, true), StringComparison.Ordinal))
                                            .ToList();

            if (usedMethodsByDeclaredType.Count == 0)
            {
                return(false);
            }

            var usedMethods = usedMethodsByDeclaredType.Where(x => x.Name.Equals(method.Name, StringComparison.Ordinal)).ToList();

            if (usedMethods.Count == 0)
            {
                return(false);
            }

            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var usedMethod in usedMethods)
            {
                var usedMethodParameters = usedMethod.GetParameters();
                if (((method.ReturnType.IsGenericParameter && usedMethod.ReturnType != typeof(void)) ||
                     usedMethod.ReturnType == method.ReturnType) &&
                    usedMethodParameters.Length == methodParameters.Length)
                {
                    return(!usedMethodParameters.Where((t, i) => !t.Name.Equals(methodParameters[i].Name, StringComparison.Ordinal)).Any());
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public void BeautifyName_WithParameters(string expected, MethodInfo methodInfo, bool useFullName, bool useHtmlFormat, bool includeReturnType)
        {
            // Act
            var actual = methodInfo.BeautifyName(useFullName, useHtmlFormat, includeReturnType);

            // Assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 3
0
        public void BeautifyName(string expected, MethodInfo methodInfo)
        {
            // Act
            var actual = methodInfo.BeautifyName();

            // Assert
            Assert.Equal(expected, actual);
        }
Ejemplo n.º 4
0
        internal static string GetDebugMethodInfo(MethodInfo method)
        {
            var debugMethodInfo = $"{method.DeclaringType?.BeautifyName()} # {method.BeautifyName(false, false, true)}";
            var c1 = method.DeclaringType != null && method.DeclaringType.IsGenericType;
            var c2 = method.DeclaringType != null && method.DeclaringType.IsGenericTypeDefinition;
            var m1 = method.IsGenericMethod;
            var m2 = method.IsGenericMethodDefinition;
            int p1C = 0, p2C = 0, p3C = 0, p4C = 0, p5C = 0, p6C = 0;
            var parameters = method.GetParameters();

            if (parameters.Length > 0)
            {
                p1C = parameters.Count(x => x.IsIn);
                p2C = parameters.Count(x => x.IsOut);
                p3C = parameters.Count(x => x.IsRetval);
                p4C = parameters.Count(x => x.IsOptional);
                p5C = parameters.Count(x => x.ParameterType.IsAbstract);
                p6C = parameters.Count(x => x.ParameterType.IsInheritedFrom(typeof(Delegate)));
            }

            var sb = new StringBuilder();

            if (c1)
            {
                sb.Append(", GenericType");
            }

            if (c2)
            {
                sb.Append(", GenericTypeDefinition");
            }

            if (m1)
            {
                sb.Append(", GenericMethod");
            }

            if (m2)
            {
                sb.Append(", GenericMethodDefinition");
            }

            if (p1C > 0)
            {
                sb.Append($", In({p1C})");
            }

            if (p2C > 0)
            {
                sb.Append($", Out({p2C})");
            }

            if (p3C > 0)
            {
                sb.Append($", RetVal({p3C})");
            }

            if (p4C > 0)
            {
                sb.Append($", Optional({p4C})");
            }

            if (p5C > 0)
            {
                sb.Append($", IsAbstract({p5C})");
            }

            if (p6C > 0)
            {
                sb.Append($", Delegate({p6C})");
            }

            if (sb.Length > 0)
            {
                debugMethodInfo += sb;
            }

            return(debugMethodInfo);
        }