etoisarobot
etoisarobot

Reputation: 7814

How to retrieve the value of a Custom Attribute from a DLL Loaded at runtime?

I have a app that requires a dll to be loaded at runtime and I want to create some Custom Attributes in the dynamically loaded DLL so when it is loaded I can check to make sure that certain attributes have certain values before trying to use it.

I create an attribute like this

using System;
[AttributeUsage(AttributeTargets.Class)]
public class ValidReleaseToApp : Attribute
{
    private string ReleaseToApplication;

    public ValidReleaseToApp(string ReleaseToApp)
    {
        this.ReleaseToApplication = ReleaseToApp;
    }
} 

In the dynamically loaded DLL I set the attribute like this

[ValidReleaseToApp("TheAppName")]
public class ClassName : IInterfaceName
etc... etc....

But when I try and read the Attribute Value I only get the Attribute Name "ValidReleaseToApp" How do I retrieve the Value "TheAppName"?

Assembly a = Assembly.LoadFrom(PathToDLL);
Type type = a.GetType("Namespace.ClassName", true);
System.Reflection.MemberInfo info = type;
var attributes = info.GetCustomAttributes(true);
MessageBox.Show(attributes[0].ToString());

Update:

Since I am Dynamically loading the dll at runtime the definition of the Attribute is not avail. to the Main App. So when I try to do the following as suggested

string value = ((ValidReleaseToApp)attributes[0]).ReleaseToApplication;
MessageBox.Show(value);

I get this error

The type or namespace name 'ValidReleaseToApp' could not be found

Update2:

OK so the problem was that I defined the Attribute within the project of the dynamically loaded DLL. Once I moved the Attribute definitions to it's own project and Added a reference to that project to both the Main Project and that of the Dynamically loaded dll The suggested code worked.

Upvotes: 2

Views: 3504

Answers (2)

Dr Herbie
Dr Herbie

Reputation: 3940

Once you have the custom attributes, you can cast them to instances of the attribute class and access their proerties:

object[] attributes = info.GetCustomAttributes(typeof(ValidReleaseToAppAttribute), true);
ValidReleaseToAppAttrigute attrib = attributes[0] as ValidReleaseToAppAttribute;
MessageBox.Show(attrib.ReleaseToApp);

Upvotes: 0

Jim Schubert
Jim Schubert

Reputation: 20357

This should work, I don't have an example in front of me right now, but it looks right. You're basically skipping the steps of exposing the property you want access to, and casting to the attribute type to retrieve that property.

using System;
[AttributeUsage(AttributeTargets.Class)]
public class ValidReleaseToApp : Attribute
{
    private string _releaseToApplication;
    public string ReleaseToApplication { get { return _releaseToApplication; } }

    public ValidReleaseToApp(string ReleaseToApp)
    {
        this._releaseToApplication = ReleaseToApp;
    }
} 


Assembly a = Assembly.LoadFrom(PathToDLL);
Type type = a.GetType("Namespace.ClassName", true);
System.Reflection.MemberInfo info = type;
var attributes = info.GetCustomAttributes(true);
if(attributes[0] is ValidReleaseToApp){
   string value = ((ValidReleaseToApp)attributes[0]).ReleaseToApplication ;
   MessageBox.Show(value);
}

Upvotes: 4

Related Questions