If you have a member for which you want to get the custom attributes and the constructor arguments, you can use the following reflection code:
MemberInfo member; // <-- Get a member
var customAttributes = member.GetCustomAttributesData();
foreach (var data in customAttributes)
{
// The type of the attribute,
// e.g. "SomeCustomAttribute"
Console.WriteLine(data.AttributeType);
foreach (var arg in data.ConstructorArguments)
{
// The type and value of the constructor arguments,
// e.g. "System.String a supplied value"
Console.WriteLine(arg.ArgumentType + " " + arg.Value);
}
}
To get a member, start with getting the type. There are two ways to get a type.
- If you have an instance
obj
, callType type = obj.GetType();
. - If you have a type name
MyType
, doType type = typeof(MyType);
.
Then you can find, for example, a particular method. Look at the reflection documentation for more info.
MemberInfo member = typeof(MyType).GetMethod("Foo");