gpt4 book ai didi

c# - 如何修复此错误 {"Length cannot be less than zero.\r\nParameter name: length"} 在我的代码中的 c# 我正在使用带有 indexofany 的子字符串

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:34 26 4
gpt4 key购买 nike

<分区>

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

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

Console.WriteLine("返回类型:"+ 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())) );

这给了我一个错误

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com