Exemplo n.º 1
0
        public static string GetDisplayName(this MethodReference method)
        {
            var sb = new System.Text.StringBuilder();

            // Match C# syntaxis name if setter or getter
            var methodDefinition = method.Resolve();

            if (methodDefinition != null && (methodDefinition.IsSetter || methodDefinition.IsGetter))
            {
                // Append property name
                string name = methodDefinition.IsSetter ? methodDefinition.Name.Substring(4) + ".set" : methodDefinition.Name.Substring(4) + ".get";
                sb.Append(name);
                // Insert declaring type name and namespace
                sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());
                return(sb.ToString());
            }

            // Append parameters
            sb.Append("(");
            if (method.HasParameters)
            {
                for (int i = 0; i < method.Parameters.Count - 1; i++)
                {
                    sb.Append(method.Parameters[i].ParameterType.GetDisplayNameWithoutNamespace()).Append(',');
                }

                sb.Append(method.Parameters[method.Parameters.Count - 1].ParameterType.GetDisplayNameWithoutNamespace());
            }

            sb.Append(")");

            // Insert generic parameters
            if (method.HasGenericParameters)
            {
                TypeReferenceExtensions.PrependGenericParameters(method.GenericParameters, sb);
            }

            // Insert method name
            if (method.Name == ".ctor")
            {
                sb.Insert(0, method.DeclaringType.Name);
            }
            else
            {
                sb.Insert(0, method.Name);
            }

            // Insert declaring type name and namespace
            if (method.DeclaringType != null)
            {
                sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());
            }

            return(sb.ToString());
        }
        public static string GetDisplayName(this MethodReference method)
        {
            var sb = new System.Text.StringBuilder();

            // Append parameters
            sb.Append("(");
            if (method.HasParameters)
            {
                for (int i = 0; i < method.Parameters.Count - 1; i++)
                {
                    sb.Append(method.Parameters[i].ParameterType.GetDisplayNameWithoutNamespace()).Append(',');
                }

                sb.Append(method.Parameters[method.Parameters.Count - 1].ParameterType.GetDisplayNameWithoutNamespace());
            }

            sb.Append(")");

            // Insert generic parameters
            if (method.HasGenericParameters)
            {
                TypeReferenceExtensions.PrependGenericParameters(method.GenericParameters, sb);
            }

            // Insert method name
            if (method.Name == ".ctor")
            {
                sb.Insert(0, method.DeclaringType.Name);
            }
            else
            {
                sb.Insert(0, method.Name);
            }

            // Insert declaring type name and namespace
            sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());

            return(sb.ToString());
        }
Exemplo n.º 3
0
        public static string GetDisplayName(this MethodReference method)
        {
            var sb = new System.Text.StringBuilder();

            // Match C# syntaxis name if setter or getter
            var methodDefinition = method.Resolve();

            if (methodDefinition != null && (methodDefinition.IsSetter || methodDefinition.IsGetter))
            {
                // Append property name
                string name = methodDefinition.IsSetter ? string.Concat(methodDefinition.Name.AsSpan(4), ".set") : string.Concat(methodDefinition.Name.AsSpan(4), ".get");
                sb.Append(name);
                // Insert declaring type name and namespace
                sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());
                return(sb.ToString());
            }

            if (methodDefinition != null && methodDefinition.IsEventMethod())
            {
                // Append event name
                string name = methodDefinition.SemanticsAttributes switch {
                    MethodSemanticsAttributes.AddOn => string.Concat(methodDefinition.Name.AsSpan(4), ".add"),
                    MethodSemanticsAttributes.RemoveOn => string.Concat(methodDefinition.Name.AsSpan(7), ".remove"),
                    MethodSemanticsAttributes.Fire => string.Concat(methodDefinition.Name.AsSpan(6), ".raise"),
                    _ => throw new NotSupportedException(),
                };
                sb.Append(name);
                // Insert declaring type name and namespace
                sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());
                return(sb.ToString());
            }

            // Append parameters
            sb.Append("(");
            if (method.HasParameters)
            {
                for (int i = 0; i < method.Parameters.Count - 1; i++)
                {
                    sb.Append(method.Parameters[i].ParameterType.GetDisplayNameWithoutNamespace()).Append(", ");
                }

                sb.Append(method.Parameters[method.Parameters.Count - 1].ParameterType.GetDisplayNameWithoutNamespace());
            }

            sb.Append(")");

            // Insert generic parameters
            if (method.HasGenericParameters)
            {
                TypeReferenceExtensions.PrependGenericParameters(method.GenericParameters, sb);
            }

            // Insert method name
            if (method.Name == ".ctor")
            {
                sb.Insert(0, method.DeclaringType.Name);
            }
            else
            {
                sb.Insert(0, method.Name);
            }

            // Insert declaring type name and namespace
            if (method.DeclaringType != null)
            {
                sb.Insert(0, '.').Insert(0, method.DeclaringType.GetDisplayName());
            }

            return(sb.ToString());
        }