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"}