Search code examples
c#reflectiondllpropertyinfo

How can I read attributes of a class in an external dll file in c#?


I have the following block of code. How can I get all the attribute names from a specific DLL file? Currently, I can get the class names, namespace but I do not know how to get attributes in the class. Thank you,

foreach (Type type in myAssambly.GetTypes())
{
    PropertyInfo myPI = type.GetProperty("DefaultModifiers");
    System.Reflection.PropertyAttributes myPA = myPI.Attributes;

    MessageBox.Show(myPA.ToString());
}

Solution

  • It sounds like really you're interested in properties:

    foreach (Type type in myAssembly.GetTypes())
    {
        foreach (PropertyInfo property in type.GetProperties())
        {
            MessageBox.Show(property.Name + " - " + property.PropertyType);
        }
    }
    

    EDIT: Okay, so it sounds like you really really want fields:

    foreach (Type type in myAssembly.GetTypes())
    {
        foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | 
                                                   BindingFlags.Static |
                                                   BindingFlags.Public |
                                                   BindingFlags.NonPublic))
        {
            MessageBox.Show(field.Name + " - " + field.FieldType);
        }
    }