Click here to Skip to main content
15,896,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there.
i have a class AIName extends Attribute like this:

C#
public class AIName : Attribute
    {
        /// <summary>
        /// Return name of AI
        /// </summary>
        /// <returns></returns>
        private string name;
        public AIName(string name)
        {
            this.name = name;
        }
        public string GetAIName()
        {
            return name;
        }
    }



and few class using custom attribute like this:
C#
[AIName("aggressive")]
    public class AggressiveNpcAI2 : GeneralNpcAI2
    {
    }

[AIName("npc")]
    public class NpcAI2 : AITemplate
    {
    }


So i would like to get all custom attribute where classes using AIName attribute.

What I have tried:

I tried this code below but its just can get 1 class that i give class name only.

C#
public void Init()
        {
            Type type = typeof(AggressiveNpcAI2);
            object[] attributes = type.GetCustomAttributes(true);
            foreach (object attribute in attributes)
            {
                AIName da = attribute as AIName;
                if (da != null)
                    Console.WriteLine("AI Name: {0}", da.GetAIName());
                else
                    Console.WriteLine();
            }
        }
Posted
Updated 8-Dec-16 5:15am
Comments
Simon_Whale 8-Dec-16 10:36am    
Is there only one attribute per class?
EADever 8-Dec-16 10:38am    
Yes, only one attribute per class.

If you can do it for one, named type then you can do it for all the types in your assembly. This will list them all:
C#
Assembly myAssembly = Assembly.GetExecutingAssembly();
Type[] types = myAssembly.GetTypes();
foreach (Type type in types)
    {
    Console.WriteLine(type.FullName);
    }
So you can combine that with your code to find them all.
 
Share this answer
 
Comments
EADever 8-Dec-16 10:58am    
can you give more details?
OriginalGriff 8-Dec-16 11:04am    
What more details do you need?
You have code you wrote to check if a specific type is decorated with your attribute, and I gave you code to find all the types in your assembly.
So what is the problem with putting the two bits of code together?
Thanks @OriginalGriff for your sugguestion.
Im done my code and its work well:

C#
Assembly myAssembly = Assembly.GetExecutingAssembly();
            Type[] types = myAssembly.GetTypes();
            foreach (Type typeA in types)
            {
                foreach (object attribute in typeA.GetCustomAttributes(true))
                {
                    AIName da = attribute as AIName;
                    if (da != null)
                    {
                        Add<AggressiveNpcAI2>(da.GetAIName());
                        Console.WriteLine("AI Name: {0}", da.GetAIName());
                    }
                }
            }
 
Share this answer
 
To add to what Griff has given

C#
Assembly asm = Assembly.GetExecutingAssembly();
foreach(Type type in asm.GetTypes())
{
  //Now if your classes are in a different folder in the same assembly then
  //you will need to specify the full name space address for example
  //"projectName.Classes" (I have assumed that you have a folder in the app called Classes)
  if(type.Namespace == "full namespace address here")
  {
    //do what is needed here
  }
}


if you are having to load the files from an external assembly then change the first line to

C#
Assembly asm - Assembly.LoadFromFile("path to dll");


reflection - Loading DLLs at runtime in C# - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900