This should work:
public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Func<TAttribute, object> pred, object oValue) {
foreach (Type type in aAssembly.GetTypes()) {
foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) {
if (Equals(pred(oTemp), oValue)) {
return type;
}
}
}
return typeof(string); //otherwise return a string type
}
Or even nicer:
public static Type GetTypeWithAttributeValue<TAttribute>(Assembly aAssembly, Predicate<TAttribute> pred) {
foreach (Type type in aAssembly.GetTypes()) {
foreach (TAttribute oTemp in type.GetCustomAttributes(typeof(TAttribute), true)) {
if (pred(oTemp)) {
return type;
}
}
}
return typeof(string); //otherwise return a string type
}
This is how the invocation looks like:
GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(), attribute => attribute.Value,
"string");
and this one respectively:
GetTypeWithAttributeValue<DefaultValueAttribute>(Assembly.GetCallingAssembly(),
attribute => attribute.Value == "string");