你用CustomAttributeBuilder
. 例如:
var cab = new CustomAttributeBuilder(
ctor, ctorArgs,
props, propValues,
fields, fieldValues
);
prop.SetCustomAttribute(cab);
(或相关的重载之一)
在您的情况下,这看起来(这纯粹是猜测)类似于:
var attribType = typeof(DisplayAttribute);
var cab = new CustomAttributeBuilder(
attribType.GetConstructor(Type.EmptyTypes), // constructor selection
new object[0], // constructor arguments - none
new[] { // properties to assign to
attribType.GetProperty("Order"),
attribType.GetProperty("ResourceType"),
attribType.GetProperty("Name"),
},
new object[] { // values for property assignment
28,
typeof(CommonResources),
"DisplayComment"
});
prop.SetCustomAttribute(cab);
注意我假设Order
,ResourceType
和Name
是属性。如果它们是fields,则存在不同的重载。