Question

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());
}
Was it helpful?

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);
    }
}

OTHER TIPS

If you have a compile-time reference to the DLL you can use a type from it to get its assembly, and then use your code to get attributes:

var myAssembly = Assembly.GetAssembly(typeof(SomeType));

Otherwise you can load it dynamically:

var myAssembly = Assembly.LoadFrom(assemblyPath);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top