NServiceBus/NServiceBus — Issue #187: Properties of attributes at generation of types of messages aren't copied
Author: antonternavsky
Created: 2012-01-27 11:09:45+00:00
State: open

For a solution of a problem - it is necessary to modify https://github.com/NServiceBus/NServiceBus/blob/master/src/impl/messageInterfaces/NServiceBus.MessageInterfaces.MessageMapper.Reflection/MessageMapper.cs
line 207, having replaced a method code "AddCustomAttributeToProperty"
private void AddCustomAttributeToProperty(object customAttribute, PropertyBuilder propBuilder)
{
var classConstructorInfo = customAttribute.GetType().GetConstructor(new Type[] { });
if (classConstructorInfo == null)
{
Logger.Warn(string.Format("Ignoring attribute '{0}' on property {1}.{2} as it has no parameterless .ctor.", customAttribute.GetType(), propBuilder.DeclaringType.Name, propBuilder.Name));
return;
}

var ignoredProperties = new[] { "TypeId" };

var propertyDescriptors =
customAttribute.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(propertyInfo => !ignoredProperties.Contains(propertyInfo.Name))
.Select(
propertyInfo =>
new
{
PropertyInfo = propertyInfo,
Value = propertyInfo.GetValue(customAttribute, new object[] { })
})
.Where(descriptor => descriptor.Value != null);

foreach (var propertyDescriptor in propertyDescriptors)
{
if (!propertyDescriptor.PropertyInfo.CanWrite)
{
throw new NotSupportedException(
string.Format(
"Attribute {0} is not supported. Setter for property {1} is not found.",
customAttribute.GetType(),
propertyDescriptor.PropertyInfo.Name));
}
}

var customAttributeBuilder = new CustomAttributeBuilder(
classConstructorInfo,
new object[] { },
propertyDescriptors.Select(descriptor => descriptor.PropertyInfo).ToArray(),
propertyDescriptors.Select(descriptor => descriptor.Value).ToArray());

propBuilder.SetCustomAttribute(customAttributeBuilder);
}