0

this is my code in C# which shows the list of class name, class type, method name, method type, return type alias names and access specifier

this is my code which shows the list of the class name, class type, method name, method type, and access specifier but I need to show the return type alias name in output I tried using substring to get the it

Console.WriteLine("Return Type: " + methodItem.ReturnType.Name.Substring(0, methodItem.ReturnType.Name.IndexOfAny("0123456789".ToCharArray()))); but got the error as

C#

public class Learndll
{
    public int add(int a, int b)
    {
        return (a + b);
    }
}
public class learn
{
    public int add(int a, int b)
    {
        return (a + b);
    }
}
public class B
{
    public int sub(int c, int d)
    {
        return (c - d);
    }
}

public class c
{
    public double mul(int s, int k)
    {
        return (s * k);
    }
}

public class d
{
    public float div(int x, int y)
    {
        return (x / y);
    }

}
class Program
{
    static void Main(string[] args)
    {

        List<MethodInfo> methods;

        // print assembly name
        Assembly a = Assembly.GetExecutingAssembly();
        Console.WriteLine("Assembly Name :" + a.FullName);

        //Get the class details

        List<Type> classDetails = a.GetTypes().ToList();

        //string List to store property Names
        List<string> PropertyName = new List<string>();

        //List to store method name
        List<string> MethodNames = new List<string>();

        //List to store Return types Type

        List<string> ReturnTypes = new List<string>();
        //Type.GetType(typeof(string).ToString());
        // to store classes
        List<string> classNames = new List<string>();


        // List to store class type
        List<string> classTypes = new List<string>();
        Type[] types = a.GetTypes();

        foreach (var item in classDetails)
        {
            if (item.IsAbstract)
            {
                Console.WriteLine("Abstract Class : " + item.Name);

            }

            else if (item.IsPublic)
            {
                Console.WriteLine("Public Class : " + item.Name);

            }

            else if (item.IsSealed)
            {
                Console.WriteLine("Sealed Class : " + item.Name);

            }
            classNames.Add(item.Name);

            Console.WriteLine("Class Name:" + item.Name);
            Console.WriteLine("Class Type:" + (item.IsPublic ? "Public" : "Private"));

            //To get the list of methods
            methods = item.GetMethods().ToList();
            List<PropertyInfo> properties = item.GetProperties().ToList();

            foreach (var property in properties)
            {
                if (property.Module.Assembly == a)
                {
                    //Storing Property Names

                    PropertyName.Add(property.Name);

                    //Console.WriteLine("Property Name :" + property.Name);
                    //.WriteLine("Property Type:" + property.PropertyType);
                }
            }
            foreach (var methodItem in methods)
            {
                if (methodItem.Module.Assembly == a)
                {
                    //storing Method Names to list
                    MethodNames.Add(methodItem.Name);
                    //ReturnTypes.Add(methodItem.ReturnType);

                    //to remove system from the name need to change its type to string 
                    ReturnTypes.Add(methodItem.ReturnType.Name);

                    //classTypes.Add(methodItem.);
                    Console.WriteLine("Method Name: " + methodItem.Name);

                    //Console.WriteLine("Return Type: " + methodItem.ReturnType.Name);
                    Console.WriteLine("Return Type: " + methodItem.ReturnType.Name.Substring(0, methodItem.ReturnType.Name.IndexOfAny("0123456789".ToCharArray())));
                    //above line is the one which show error
                    Console.WriteLine("Access modifier: " + (methodItem.IsPublic ? "Public" : methodItem.IsPrivate ? "Private" : methodItem.IsAssembly ? "Internal" : methodItem.IsFamily ? "Protected" : "NA"));
                    Console.WriteLine("--------------------------------");

                }
            }


        }

I expect the output of it to be int, double to be double and float to be float

so i used substring in my code Console.WriteLine("Return Type: " + methodItem.ReturnType.Name.Substring(0, methodItem.ReturnType.Name.IndexOfAny("0123456789".ToCharArray())));

which gave me an error

{"Length cannot be less than zero.\r\nParameter name: length"}

Ratan Uday Kumar
  • 3,556
  • 5
  • 26
  • 49
  • IndexOfAny is returning -1; in other words, there are no numbers. – brijber Jun 20 '19 at 06:24
  • Welcome to Stack Overflow. I would suggest that you could reduce your 136 lines of code here to just 2 - one that sets a variable called `name`, and one that prints `name.Substring(name.IndexOfAny("0123456789".ToCharArray()))`. It's always a good idea to post a question with a [mcve] - most of the code you've got here is irrelevant to the problem you're facing, and so doesn't need to be in the question. – Jon Skeet Jun 20 '19 at 06:25

1 Answers1

3

Let's visit the documentation:

String.IndexOfAny Method

Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters. The method returns -1 if the characters in the array are not found in this instance.

String.Substring Method

Exceptions

ArgumentOutOfRangeException startIndex plus length indicates a position not within this instance.

-or-

startIndex or length is less than zero.

So you can see what's happening, how do we fix that? Well, the first thing is not to assume your characters are actually present.

var index = ...IndexOfAny("0123456789");

if (index < 0)
   Console.WriteLine("OMG!!! Error you must have");
halfer
  • 18,701
  • 13
  • 79
  • 158
TheGeneral
  • 69,477
  • 8
  • 65
  • 107