I'm trying to access all the user declared classes in my project. My goal is calling functions of the classes that I access. I have researched this for 2-3 days, but I couldn't find any solutions.
I have tried to get types from assembly but it gave me so complicated results. Here is what I tried:
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies();
Type[] types = assembly.GetTypes();
int i;
for ( i = 0 ; i < types.Length ; i++ )
{
Console.WriteLine( types[ i ].Name );
}
Quick Example - If any other programmer that works on the project creates a class called "Hello", I need to get that class and call the required function inside of it.
I'm stuck with the "getting user/programmer declared classes" part, any help is great.
Update: Thanks to everyone for helping me out. I managed the solve this problem by creating a custom attribute just like how @Ghost4Man suggests. My new code looks like this:
public void Test()
{
foreach ( Assembly a in AppDomain.CurrentDomain.GetAssemblies() )
{
Type[] array = a.GetTypes();
for ( int i = 0 ; i < array.Length ; i++ )
{
Type t = array[ i ];
DConsoleRequired dConsoleRequired = ( DConsoleRequired )
t.GetCustomAttributes( typeof( DConsoleRequired ) , false )[ 0 ];
if ( dConsoleRequired != null )
{
Debug.Log( t.Name );
}
}
}
}
Update 2 : Updated code
public void Test2()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Type[] types = new Type[ assemblies.Length ];
int i;
for ( i = 0 ; i < assemblies.Length ; i++ )
{
types = assemblies[ i ].GetTypes();
for ( int j = 0 ; j < types.Length ; j++ )
{
var type = types[ j ];
if ( ConsoleRequiredAttribute.IsDefined( type , typeof( ConsoleRequiredAttribute ) ) )
{
Debug.Log( type.Name );
}
}
}
}