NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Properties of attributes at generation of types of messages aren't copied

Open antonternavsky opened this issue 14 years ago • 1 comments

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);
    }

antonternavsky avatar Jan 27 '12 11:01 antonternavsky

Hi Anton, I tried your code and got an exception. Did you try it?

shlomii avatar Mar 05 '12 18:03 shlomii