Пример #1
0
        internal static MethodDisplayInfo?GetMethodDisplayString(MethodBase?method)
        {
            // Special case: no method available
            if (method == null)
            {
                return(null);
            }

            // Type name
            var type = method.DeclaringType;

            var methodName = method.Name;

            string?subMethod = null;

            if (type != null && type.IsDefined(typeof(CompilerGeneratedAttribute)) &&
                (typeof(IAsyncStateMachine).IsAssignableFrom(type) || typeof(IEnumerator).IsAssignableFrom(type)))
            {
                // Convert StateMachine methods to correct overload +MoveNext()
                if (TryResolveStateMachineMethod(ref method, out type))
                {
                    subMethod = methodName;
                }
            }

            string?declaringTypeName = null;

            // ResolveStateMachineMethod may have set declaringType to null
            if (type != null)
            {
                declaringTypeName = TypeNameHelper.GetTypeDisplayName(type, includeGenericParameterNames: true);
            }

            string?genericArguments = null;

            if (method.IsGenericMethod)
            {
                genericArguments = "<" + string.Join(", ", method.GetGenericArguments()
                                                     .Select(arg => TypeNameHelper.GetTypeDisplayName(arg, fullName: false, includeGenericParameterNames: true))) + ">";
            }

            // Method parameters
            var parameters = method.GetParameters().Select(parameter =>
            {
                var parameterType = parameter.ParameterType;

                var prefix = string.Empty;
                if (parameter.IsOut)
                {
                    prefix = "out";
                }
                else if (parameterType != null && parameterType.IsByRef)
                {
                    prefix = "ref";
                }

                var parameterTypeString = "?";
                if (parameterType != null)
                {
                    if (parameterType.IsByRef)
                    {
                        parameterType = parameterType.GetElementType();
                    }

                    parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType !, fullName: false, includeGenericParameterNames: true);
                }

                return(new ParameterDisplayInfo
                {
                    Prefix = prefix,
                    Name = parameter.Name,
                    Type = parameterTypeString,
                });
            });

            var methodDisplayInfo = new MethodDisplayInfo(declaringTypeName, method.Name, genericArguments, subMethod, parameters);

            return(methodDisplayInfo);
        }
Пример #2
0
        internal static MethodDisplayInfo GetMethodDisplayString(MethodBase method)
        {
            // Special case: no method available
            if (method == null)
            {
                return(null);
            }

            var methodDisplayInfo = new MethodDisplayInfo();

            // Type name
            var type = method.DeclaringType;

            if (type != null)
            {
                methodDisplayInfo.DeclaringTypeName = TypeNameHelper.GetTypeDisplayName(type);
            }

            // Method name
            methodDisplayInfo.Name = method.Name;
            if (method.IsGenericMethod)
            {
                var genericArguments = string.Join(", ", method.GetGenericArguments()
                                                   .Select(arg => TypeNameHelper.GetTypeDisplayName(arg, fullName: false)));
                methodDisplayInfo.GenericArguments += "<" + genericArguments + ">";
            }

            // Method parameters
            methodDisplayInfo.Parameters = method.GetParameters().Select(parameter =>
            {
                var parameterType = parameter.ParameterType;

                var prefix = string.Empty;
                if (parameter.IsOut)
                {
                    prefix = "out";
                }
                else if (parameterType != null && parameterType.IsByRef)
                {
                    prefix = "ref";
                }

                var parameterTypeString = "?";
                if (parameterType != null)
                {
                    if (parameterType.IsByRef)
                    {
                        parameterType = parameterType.GetElementType();
                    }

                    parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType, fullName: false);
                }

                return(new ParameterDisplayInfo
                {
                    Prefix = prefix,
                    Name = parameter.Name,
                    Type = parameterTypeString,
                });
            });

            return(methodDisplayInfo);
        }