How to Read an Attribute on a Class at Runtime

How do I read an attribute on a class at runtime?

public string GetDomainName<T>()
{
var dnAttribute = typeof(T).GetCustomAttributes(
typeof(DomainNameAttribute), true
).FirstOrDefault() as DomainNameAttribute;
if (dnAttribute != null)
{
return dnAttribute.Name;
}
return null;
}

UPDATE:

This method could be further generalized to work with any attribute:

public static class AttributeExtensions
{
public static TValue GetAttributeValue<TAttribute, TValue>(
this Type type,
Func<TAttribute, TValue> valueSelector)
where TAttribute : Attribute
{
var att = type.GetCustomAttributes(
typeof(TAttribute), true
).FirstOrDefault() as TAttribute;
if (att != null)
{
return valueSelector(att);
}
return default(TValue);
}
}

and use like this:

string name = typeof(MyClass)
.GetAttributeValue((DomainNameAttribute dna) => dna.Name);

Read attribute value of derived class

Attributes is a properties of a class, so you don't need to create an instance of this class to get those values. You can create a separate helper-class for this:

internal static class DBHelpers
{
public static string GetTableName<TDBItem>()
where TDBItem : DBItem
{
var attribute = typeof(TDBItem).GetCustomAttribute<DBItemAttribute>();
return attribute?.TableName;
}
}

So now you can use it like this:

var tableName1 = DBHelpers.GetTableName<DBHelpers>();
var tableName2 = DBHelpers.GetTableName<Book>();

How to add only an attribute on a class at runtime?

As rpgmaker already said in a comment, you can't modify a class after it's loaded. There are some ways around that:

  1. Use Mono Cecil to modify existing assembly.
  2. Use something like PostSharp as a post-build step to add the attributes to the assembly.
  3. Create a new class at runtime, that is basically a copy of your class with the attributes added.
  4. Use something like Roslyn to modify your source code before you actually compile it.

But all of them are just workarounds. And your end goal is not actually to add attributes to a file, it seems it's to read a CSV file using FileHelpers without having to specify the necessary attributes.

There is a page on the FileHelpers site describing how to do that, using several different approaches, including loading the format from XML.

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

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


Related Topics



Leave a reply



Submit