首页 > 解决方案 > 如何修复此错误 {"Length cannot be less than zero.\r\nParameter name: length"} in c# in my code 我正在使用带有 indexofany 的子字符串

问题描述

这是我在 C# 中的代码,它显示了类名、类类型、方法名、方法类型、返回类型别名和访问说明符的列表

这是我的代码,它显示了类名、类类型、方法名、方法类型和访问说明符的列表,但我需要在输出中显示返回类型别名我尝试使用子字符串来获取它

Console.WriteLine("Return Type: " + methodItem.ReturnType.Name.Substring(0, methodItem.ReturnType.Name.IndexOfAny("0123456789".ToCharArray()))); 但得到的错误为

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("--------------------------------");

                }
            }


        }

我希望它的输出是 int,double 是 double,float 是 float

所以我在我的代码中使用了子字符串Console.WriteLine("Return Type: " + methodItem.ReturnType.Name.Substring(0, methodItem.ReturnType.Name.IndexOfAny("0123456789".ToCharArray())));

这给了我一个错误

{"长度不能小于零。\r\n参数名称:长度"}

标签: c#.net

解决方案


让我们访问文档:

String.IndexOfAny 方法

报告指定 Unicode 字符数组中任何字符在此实例中第一次出现的索引。如果在此实例中未找到数组中的字符,则该方法 返回 -1 。

String.Substring 方法

例外

ArgumentOutOfRangeExceptionstartIndex 加上长度表示不在此实例中的位置。

-或者-

startIndex 或长度小于零。

所以你可以看到发生了什么,我们如何解决这个问题?嗯,首先不要假设你的角色确实存在。

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

if (index < 0)
   Console.WriteLine("OMG!!! Error you must have");

推荐阅读