Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Deliverystack.Core.Attributes
- {
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Reflection;
- /// <summary>
- /// Attribute to decorate JsonConverters, mainly to manage modular blocks,
- /// so that SerializationConfigurator can add them to the JsonSerializerSettings
- /// exposed by ContentstackClient and used to instantiate objects from Contentstack JSON.
- /// [AutoLoadJsonConverter(true)]
- /// </summary>
- [AttributeUsage(AttributeTargets.Class)]
- public class AutoLoadJsonConverter : AttributeBase
- {
- #region Fields
- private static List<Type> _types;
- #endregion
- #region Properties
- /// <summary>
- /// Enabled by default.
- /// </summary>
- public bool Enabled { get; }
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="enabled"></param>
- public AutoLoadJsonConverter(bool enabled = true)
- {
- Enabled = enabled;
- }
- #endregion
- public static bool IsEnabledForType(Type t)
- {
- Trace.Assert(t != null, "t is null");
- foreach (var attr in t.GetCustomAttributes(typeof(AutoLoadJsonConverter)))
- {
- AutoLoadJsonConverter ctdAttr = attr as AutoLoadJsonConverter;
- Trace.Assert(ctdAttr != null, "cast is null");
- if (!ctdAttr.Enabled)
- {
- return ctdAttr.Enabled;
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement