The program gets information about a class such as method/feilds/properties names return types parametres using reflection. Now I let the user type in the name and see the data. When working with the .Net libraries or my own assemblies works fine. But when I ask it to find stuffs from a different assembly that I made it just cant find it. Here are the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace CSharpVenkat
{
class Program
{
static void DataFinder()
{
try
{
//Using bool as I didnt want to make a nested for loop
bool consoleColorSet = false;
for (;;)
{
if (consoleColorSet == false)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.Clear();
consoleColorSet = true;
}
Console.Write("Class: ");
string s = Console.ReadLine();
Console.WriteLine();
Type t = Type.GetType(s) as Type;
if (t != null)
{
//Methods
MethodInfo[] methods = t.GetMethods();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Methods of class \"" + t.FullName + "\"");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
if (methods.Length <= 0)
{
Console.WriteLine("None!");
}
foreach (MethodInfo method in methods)
{
Console.WriteLine(method.ToString());
}
Console.WriteLine();
Console.WriteLine();
//Constructors
ConstructorInfo[] constructors = t.GetConstructors();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Constructor of class \"" + t.FullName + "\"");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
if (constructors.Length <= 0)
{
Console.WriteLine("None!");
}
foreach (ConstructorInfo constructor in constructors)
{
Console.WriteLine(constructor.Name + " " + constructor.ToString());
}
Console.WriteLine();
Console.WriteLine();
//Fields
FieldInfo[] fields = t.GetFields();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Fields of class \"" + t.FullName + "\"");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
if (fields.Length <= 0)
{
Console.WriteLine("None!");
}
foreach (FieldInfo field in fields)
{
Console.WriteLine(field.FieldType.Name + " " + field.Name);
}
Console.WriteLine();
Console.WriteLine();
//Properties
PropertyInfo[] properties = t.GetProperties();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Properties of class \"" + t.FullName + "\"");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
if (properties.Length <= 0)
{
Console.WriteLine("None!");
}
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.PropertyType.Name + " " + property.Name);
}
break;
}
else
{
Console.WriteLine("Invalid ! Please check your namespace and class name!");
Console.WriteLine();
}
}
}
//Really Incase something goes wrong, I dont know anything that could go wrong here still!
catch (Exception e)
{
Console.WriteLine("Programm stopped responding! due to error", e.GetType().Name);
}
}
//Main method
static void Main(string[] args)
{
DataFinder();
Console.ReadKey();
}
}
}
Now I know I could get the assembly of the class I am trying to execute by doing
Assembly a = Assembly.GetAssembly(typeof(ClassName));
Type t = a.GetType(s); //s is the userput
But the problem is I want to let the Console user do it not me writing the class name. I cant pass a string inside typeof
either. The problem is going like: If I want the the "Type" of the user input I need to get the assembly first but to get the assembly I need to get the "Type". How do I solve this.