-Added AttributeProvider

-Increament version to 7.0.1
This commit is contained in:
James Newton-King
2014-12-23 10:57:17 +00:00
parent d22b5fd534
commit a2e15adf80
29 changed files with 389 additions and 29 deletions
+26 -1
View File
@@ -1,4 +1,29 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@@ -1,3 +1,28 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -31,6 +56,5 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.7.18023")]
[assembly: AssemblyVersion("7.0.0.0")]
[assembly: AssemblyFileVersion("7.0.1.18023")]
@@ -101,6 +101,8 @@
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DependencyInjectionTests.cs" />
<Compile Include="Serialization\DynamicConcreteTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Serialization\FSharpTests.cs" />
@@ -110,6 +112,7 @@
<Compile Include="Serialization\MetadataPropertyHandlingTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\ShouldSerializeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
@@ -143,6 +143,8 @@
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DependencyInjectionTests.cs" />
<Compile Include="Serialization\DynamicConcreteTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Serialization\FSharpTests.cs" />
@@ -152,6 +154,7 @@
<Compile Include="Serialization\MetadataPropertyHandlingTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\ShouldSerializeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
@@ -265,6 +265,8 @@
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DependencyInjectionTests.cs" />
<Compile Include="Serialization\DynamicConcreteTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Serialization\FSharpTests.cs" />
@@ -272,6 +274,7 @@
<Compile Include="Serialization\JsonSerializerCollectionsTests.cs" />
<Compile Include="Serialization\MetadataPropertyHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\ShouldSerializeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
@@ -144,6 +144,8 @@
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DependencyInjectionTests.cs" />
<Compile Include="Serialization\DynamicConcreteTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Serialization\FSharpTests.cs" />
@@ -151,6 +153,7 @@
<Compile Include="Serialization\JsonSerializerCollectionsTests.cs" />
<Compile Include="Serialization\MetadataPropertyHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\ShouldSerializeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
@@ -145,6 +145,8 @@
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DependencyInjectionTests.cs" />
<Compile Include="Serialization\DynamicConcreteTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Serialization\FSharpTests.cs" />
@@ -152,6 +154,7 @@
<Compile Include="Serialization\JsonSerializerCollectionsTests.cs" />
<Compile Include="Serialization\MetadataPropertyHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\ShouldSerializeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" />
@@ -261,6 +261,7 @@
<Compile Include="Serialization\PopulateTests.cs" />
<Compile Include="Serialization\PreserveReferencesHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\ReflectionAttributeProviderTests.cs" />
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
<Compile Include="Serialization\SerializationEventAttributeTests.cs" />
<Compile Include="Serialization\JsonSerializerCollectionsTests.cs" />
@@ -75,5 +75,5 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.7.18023")]
[assembly: AssemblyVersion("7.0.0.0")]
[assembly: AssemblyFileVersion("7.0.1.18023")]
@@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;
#if NETFX_CORE
@@ -155,6 +156,17 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.IsNotNull(contract.DefaultCreator);
}
[Test]
public void PropertyAttributeProvider()
{
var resolver = new DefaultContractResolver();
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(Invoice));
JsonProperty property = contract.Properties["FollowUpDays"];
Assert.AreEqual(1, property.AttributeProvider.GetAttributes(false).Count);
Assert.AreEqual(typeof(DefaultValueAttribute), property.AttributeProvider.GetAttributes(false)[0].GetType());
}
[Test]
public void AbstractTestClass()
{
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !(NET35 || NET20 || ASPNETCORE50)
#if !(NET40 || NET35 || NET20 || ASPNETCORE50 || PORTABLE || PORTABLE40)
using Autofac;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
@@ -29,8 +29,11 @@ using System.Reflection.Emit;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
using System;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
@@ -46,6 +46,7 @@ using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Serialization
{
[TestFixture]
[Ignore]
public class ImmutableCollectionsTests : TestFixtureBase
{
#region List
@@ -0,0 +1,105 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests.Serialization
{
[TestFixture]
public class ReflectionAttributeProviderTests : TestFixtureBase
{
public class ReflectionTestObject
{
[DefaultValue("1")]
[JsonProperty]
public int TestProperty { get; set; }
[DefaultValue("1")]
[JsonProperty]
public int TestField;
public ReflectionTestObject(
[DefaultValue("1")]
[JsonProperty]
int testParameter)
{
TestProperty = testParameter;
TestField = testParameter;
}
}
[Test]
public void GetAttributes_Property()
{
PropertyInfo property = typeof(ReflectionTestObject).GetProperty("TestProperty");
ReflectionAttributeProvider provider = new ReflectionAttributeProvider(property);
IList<Attribute> attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false);
Assert.AreEqual(1, attributes.Count);
attributes = provider.GetAttributes(false);
Assert.AreEqual(2, attributes.Count);
}
[Test]
public void GetAttributes_Field()
{
FieldInfo field = typeof(ReflectionTestObject).GetField("TestField");
ReflectionAttributeProvider provider = new ReflectionAttributeProvider(field);
IList<Attribute> attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false);
Assert.AreEqual(1, attributes.Count);
attributes = provider.GetAttributes(false);
Assert.AreEqual(2, attributes.Count);
}
[Test]
public void GetAttributes_Parameter()
{
ParameterInfo[] parameters = typeof(ReflectionTestObject).GetConstructor(new[] { typeof(int) }).GetParameters();
ParameterInfo parameter = parameters[0];
ReflectionAttributeProvider provider = new ReflectionAttributeProvider(parameter);
IList<Attribute> attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false);
Assert.AreEqual(1, attributes.Count);
attributes = provider.GetAttributes(false);
Assert.AreEqual(2, attributes.Count);
}
}
}
-3
View File
@@ -66,9 +66,6 @@ namespace Newtonsoft.Json.Linq
private JToken _next;
private object _annotations;
//private int? _lineNumber;
//private int? _linePosition;
private static readonly JTokenType[] BooleanTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
@@ -123,10 +123,12 @@
<Compile Include="SerializationBinder.cs" />
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="Serialization\JsonDynamicContract.cs" />
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
@@ -135,9 +135,11 @@
<Compile Include="SerializationBinder.cs" />
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
@@ -135,9 +135,11 @@
<Compile Include="MetadataPropertyHandling.cs" />
<Compile Include="Serialization\DiagnosticsTraceWriter.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
<Compile Include="StringEscapeHandling.cs" />
@@ -166,6 +166,7 @@
<Compile Include="Serialization\DynamicValueProvider.cs" />
<Compile Include="Serialization\ErrorContext.cs" />
<Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
@@ -191,6 +192,7 @@
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ObjectConstructor.cs" />
<Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
@@ -171,6 +171,7 @@
<Compile Include="Serialization\ErrorContext.cs" />
<Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
@@ -196,6 +197,7 @@
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ObjectConstructor.cs" />
<Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
@@ -170,6 +170,7 @@
<Compile Include="Serialization\ErrorContext.cs" />
<Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IAttributeProvider.cs" />
<Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
@@ -195,6 +196,7 @@
<Compile Include="Serialization\MemoryTraceWriter.cs" />
<Compile Include="Serialization\ObjectConstructor.cs" />
<Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionAttributeProvider.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="Serialization\TraceJsonReader.cs" />
<Compile Include="Serialization\TraceJsonWriter.cs" />
@@ -91,6 +91,6 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.7.18023")]
[assembly: AssemblyVersion("7.0.0.0")]
[assembly: AssemblyFileVersion("7.0.1.18023")]
[assembly: CLSCompliant(true)]
@@ -640,6 +640,7 @@ namespace Newtonsoft.Json.Serialization
{
JsonProperty property = new JsonProperty();
property.PropertyType = parameterInfo.ParameterType;
property.AttributeProvider = new ReflectionAttributeProvider(parameterInfo);
bool allowNonPublicAccess;
SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
@@ -1167,6 +1168,7 @@ namespace Newtonsoft.Json.Serialization
property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
property.DeclaringType = member.DeclaringType;
property.ValueProvider = CreateMemberValueProvider(member);
property.AttributeProvider = new ReflectionAttributeProvider(member);
bool allowNonPublicAccess;
SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
@@ -0,0 +1,51 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// Provides methods to get attributes.
/// </summary>
public interface IAttributeProvider
{
/// <summary>
/// Returns a collection of all of the attributes, or an empty collection if there are no attributes.
/// </summary>
/// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param>
/// <returns>A collection of <see cref="Attribute"/>s, or an empty collection.</returns>
IList<Attribute> GetAttributes(bool inherit);
/// <summary>
/// Returns a collection of attributes, identified by type, or an empty collection if there are no attributes.
/// </summary>
/// <param name="attributeType">The type of the attributes.</param>
/// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param>
/// <returns>A collection of <see cref="Attribute"/>s, or an empty collection.</returns>
IList<Attribute> GetAttributes(Type attributeType, bool inherit);
}
}
@@ -24,6 +24,7 @@
#endregion
using System;
using System.Reflection;
using Newtonsoft.Json.Utilities;
#if NET20
@@ -87,6 +88,12 @@ namespace Newtonsoft.Json.Serialization
/// <value>The <see cref="IValueProvider"/> that will get and set the <see cref="JsonProperty"/> during serialization.</value>
public IValueProvider ValueProvider { get; set; }
/// <summary>
/// Gets or sets the <see cref="IAttributeProvider"/> for this property.
/// </summary>
/// <value>The <see cref="IAttributeProvider"/> for this property.</value>
public IAttributeProvider AttributeProvider { get; set; }
/// <summary>
/// Gets or sets the type of the property.
/// </summary>
@@ -0,0 +1,70 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// Provides methods to get attributes from a <see cref="Type"/>, <see cref="MemberInfo"/>, <see cref="ParameterInfo"/> or <see cref="Assembly"/>.
/// </summary>
public class ReflectionAttributeProvider : IAttributeProvider
{
private readonly object _attributeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="ReflectionAttributeProvider"/> class.
/// </summary>
public ReflectionAttributeProvider(object attributeProvider)
{
ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");
_attributeProvider = attributeProvider;
}
/// <summary>
/// Returns a collection of all of the attributes, or an empty collection if there are no attributes.
/// </summary>
/// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param>
/// <returns>A collection of <see cref="Attribute"/>s, or an empty collection.</returns>
public IList<Attribute> GetAttributes(bool inherit)
{
return ReflectionUtils.GetAttributes(_attributeProvider, null, inherit);
}
/// <summary>
/// Returns a collection of attributes, identified by type, or an empty collection if there are no attributes.
/// </summary>
/// <param name="attributeType">The type of the attributes.</param>
/// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param>
/// <returns>A collection of <see cref="Attribute"/>s, or an empty collection.</returns>
public IList<Attribute> GetAttributes(Type attributeType, bool inherit)
{
return ReflectionUtils.GetAttributes(_attributeProvider, attributeType, inherit);
}
}
}
@@ -683,24 +683,43 @@ namespace Newtonsoft.Json.Utilities
// ICustomAttributeProvider doesn't do inheritance
if (provider is Type)
return (Attribute[])((Type)provider).GetCustomAttributes(attributeType, inherit);
{
Type t = (Type)provider;
object[] attributes = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
return (Attribute[])attributes;
}
if (provider is Assembly)
return Attribute.GetCustomAttributes((Assembly)provider, attributeType);
{
Assembly a = (Assembly)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);
}
if (provider is MemberInfo)
return Attribute.GetCustomAttributes((MemberInfo)provider, attributeType, inherit);
{
MemberInfo m = (MemberInfo)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
}
#if !PORTABLE40
if (provider is Module)
return Attribute.GetCustomAttributes((Module)provider, attributeType, inherit);
{
Module m = (Module)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
}
#endif
if (provider is ParameterInfo)
return Attribute.GetCustomAttributes((ParameterInfo)provider, attributeType, inherit);
{
ParameterInfo p = (ParameterInfo)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
}
#if !PORTABLE40
return (Attribute[])((ICustomAttributeProvider)attributeProvider).GetCustomAttributes(attributeType, inherit);
ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider;
object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit);
return (Attribute[])result;
#else
throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));
#endif
@@ -714,19 +733,36 @@ namespace Newtonsoft.Json.Utilities
public static Attribute[] GetAttributes(object provider, Type attributeType, bool inherit)
{
if (provider is Type)
return ((Type) provider).GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray();
{
Type t = (Type)provider;
return (attributeType != null)
? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray()
: t.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
}
if (provider is Assembly)
return ((Assembly) provider).GetCustomAttributes(attributeType).ToArray();
{
Assembly a = (Assembly)provider;
return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray();
}
if (provider is MemberInfo)
return ((MemberInfo) provider).GetCustomAttributes(attributeType, inherit).ToArray();
{
MemberInfo m = (MemberInfo)provider;
return (attributeType != null) ? m.GetCustomAttributes(attributeType, inherit).ToArray() : m.GetCustomAttributes(inherit).ToArray();
}
if (provider is Module)
return ((Module) provider).GetCustomAttributes(attributeType).ToArray();
{
Module m = (Module)provider;
return (attributeType != null) ? m.GetCustomAttributes(attributeType).ToArray() : m.GetCustomAttributes().ToArray();
}
if (provider is ParameterInfo)
return ((ParameterInfo) provider).GetCustomAttributes(attributeType, inherit).ToArray();
{
ParameterInfo p = (ParameterInfo)provider;
return (attributeType != null) ? p.GetCustomAttributes(attributeType, inherit).ToArray() : p.GetCustomAttributes(inherit).ToArray();
}
throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));
}