This is not a general solution, but will work if you are willing to constrain the attributes you support to those with parameterless constructors and Read/Write Properties and Fields
CustomAttributeBuilder BuildCustomAttribute(System.Attribute attribute)
{
Type type = attribute.GetType();
var constructor = type.GetConstructor(Type.EmptyTypes);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
var propertyValues = from p in properties
select p.GetValue(attribute, null);
var fieldValues = from f in fields
select f.GetValue(attribute);
return new CustomAttributeBuilder(constructor,
Type.EmptyTypes,
properties,
propertyValues.ToArray(),
fields,
fieldValues.ToArray());
}
To do a general solution, you could use expressions. That is more complicated, but would allow syntax like:
BuildCustomAttribute(() => new ObsoleteAttribute("Demo", true));
Parsing the expression to extract the constructor info and the parameters would be the complex part, but it can be done.
CustomAttributeBuilder BuildCustomAttribute(Expression<Action> exp)
{
//extract ConstructorInfo from exp
//extract ParameterValues from exp
//extract Attribute Type from exp
return new CustomAttributeBuilder(ConstructorInfo, ParameterValues);
}