diff --git a/src/MessagePack/Attributes.cs b/src/MessagePack/Attributes.cs index c0dea5b8..e8b231aa 100644 --- a/src/MessagePack/Attributes.cs +++ b/src/MessagePack/Attributes.cs @@ -58,10 +58,17 @@ namespace MessagePack public class MessagePackFormatterAttribute : Attribute { public Type FormatterType { get; private set; } + public object[] Arguments { get; private set; } public MessagePackFormatterAttribute(Type formatterType) { this.FormatterType = formatterType; } + + public MessagePackFormatterAttribute(Type formatterType, params object[] arguments) + { + this.FormatterType = formatterType; + this.Arguments = arguments; + } } } \ No newline at end of file diff --git a/src/MessagePack/Resolvers/AttributeFormatterResolver.cs b/src/MessagePack/Resolvers/AttributeFormatterResolver.cs index 8bdc3f1b..a6a10ae0 100644 --- a/src/MessagePack/Resolvers/AttributeFormatterResolver.cs +++ b/src/MessagePack/Resolvers/AttributeFormatterResolver.cs @@ -33,7 +33,14 @@ namespace MessagePack.Resolvers return; } - formatter = (IMessagePackFormatter)Activator.CreateInstance(attr.FormatterType); + if (attr.Arguments == null) + { + formatter = (IMessagePackFormatter)Activator.CreateInstance(attr.FormatterType); + } + else + { + formatter = (IMessagePackFormatter)Activator.CreateInstance(attr.FormatterType, attr.Arguments); + } } } } diff --git a/tests/MessagePack.Tests/SpecifiedFormatterResolverTest.cs b/tests/MessagePack.Tests/SpecifiedFormatterResolverTest.cs index ce0c693e..c83d76d7 100644 --- a/tests/MessagePack.Tests/SpecifiedFormatterResolverTest.cs +++ b/tests/MessagePack.Tests/SpecifiedFormatterResolverTest.cs @@ -134,6 +134,44 @@ namespace MessagePack.Tests public int A { get; set; } } + + [MessagePackFormatter(typeof(NoObjectFormatter), CustomyEnumObject.C)] + class CustomClassObjectWithArgument + { + int X; + + public CustomClassObjectWithArgument(int x) + { + this.X = x; + } + + public int GetX() + { + return X; + } + + class NoObjectFormatter : IMessagePackFormatter + { + CustomyEnumObject x; + + public NoObjectFormatter(CustomyEnumObject x) + { + this.x = x; + } + + public CustomClassObjectWithArgument Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + { + var r = MessagePackBinary.ReadInt32(bytes, offset, out readSize); + return new CustomClassObjectWithArgument(r); + } + + public int Serialize(ref byte[] bytes, int offset, CustomClassObjectWithArgument value, IFormatterResolver formatterResolver) + { + return MessagePackBinary.WriteInt32(ref bytes, offset, value.X * (int)x); + } + } + } + T Convert(T value) { return MessagePackSerializer.Deserialize(MessagePackSerializer.Serialize(value, MessagePack.Resolvers.StandardResolver.Instance), MessagePack.Resolvers.StandardResolver.Instance); @@ -148,5 +186,11 @@ namespace MessagePack.Tests Convert((CustomyEnumObject)(1234)).Is(CustomyEnumObject.B); Convert(new HogeMoge { A = 999 }).A.Is(999); } + + [Fact] + public void WithArg() + { + Convert(new CustomClassObjectWithArgument(999)).GetX().Is(999 * 2); + } } }