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.