문제

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());
}
도움이 되었습니까?

해결책

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

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top