コード例 #1
0
        public void TestSetup()
        {
            innerBinder = Substitute.For <ISafeSettingsBinder <int> >();
            innerBinder.Bind(Arg.Any <ISettingsNode>()).Returns(
                callInfo => callInfo.Arg <ISettingsNode>()?.Value == "42" ?
                SettingsBindingResult.Success(42) :
                SettingsBindingResult.Error <int>(":("));

            binder = new NullableBinder <int>(innerBinder);
        }
コード例 #2
0
        public void TestSetup()
        {
            stringBinder = Substitute.For <ISafeSettingsBinder <string> >();
            stringBinder.Bind(Arg.Any <ISettingsNode>())
            .Returns(callInfo =>
                     callInfo.Arg <ISettingsNode>()?.Value != "BAD" ?
                     SettingsBindingResult.Success(callInfo.Arg <ISettingsNode>()?.Value) :
                     SettingsBindingResult.Error <string>(":("));

            binder = new SetBinder <string>(stringBinder);
        }
コード例 #3
0
        public void TestSetup()
        {
            stringBinder = Substitute.For <ISafeSettingsBinder <string> >();
            stringBinder.Bind(Arg.Any <ISettingsNode>())
            .Returns(callInfo => SettingsBindingResult.Success(callInfo.Arg <ISettingsNode>().Value));

            var boolBinder = Substitute.For <ISafeSettingsBinder <bool> >();

            boolBinder.Bind(Arg.Any <ISettingsNode>())
            .Returns(callInfo => (callInfo.Arg <ISettingsNode>() as ValueNode)?.Value == "true" ?
                     SettingsBindingResult.Success(true) : SettingsBindingResult.Error <bool>(":("));

            binder = new DictionaryBinder <string, bool>(stringBinder, boolBinder);
        }
コード例 #4
0
 public UnsafeBinderWrapper(ISafeSettingsBinder <T> binder) => Binder = binder;
コード例 #5
0
        public static bool TryObtainBindByBinder <T>(this MemberInfo member, bool wrap, out ISafeSettingsBinder <T> settingsBinder)
        {
            settingsBinder = null;

            var attribute = AttributeHelper.Get <BindByAttribute>(member);

            if (attribute == null)
            {
                return(false);
            }

            var memberType = GetMemberType(member);

            var desiredBinderType = typeof(ISettingsBinder <>).MakeGenericType(memberType);

            if (!desiredBinderType.IsAssignableFrom(attribute.BinderType))
            {
                throw new InvalidOperationException($"The type specified in {nameof(BindByAttribute)} must implement {desiredBinderType.ToFriendlyName()}.");
            }

            var binder = Activator.CreateInstance(attribute.BinderType);

            binder = Activator.CreateInstance(typeof(SafeBinderWrapper <>).MakeGenericType(memberType), binder);

            if (wrap)
            {
                binder = Activator.CreateInstance(typeof(BinderWrapper <>).MakeGenericType(memberType), binder);
            }

            settingsBinder = (ISafeSettingsBinder <T>)binder;
            return(true);
        }
コード例 #6
0
 protected CollectionBinder(ISafeSettingsBinder <TValue> elementBinder) =>
 this.elementBinder = elementBinder;
コード例 #7
0
        public void SetupCustomBinder <TValue>(ISafeSettingsBinder <TValue> binder)
        {
            EnsureCanRegisterBinders();

            customBinderRegistrationsBefore.Enqueue(container => container.RegisterInstance(typeof(ISafeSettingsBinder <TValue>), binder));
        }
コード例 #8
0
 public ListBinder(ISafeSettingsBinder <T> elementBinder)
     : base(elementBinder)
 {
 }
コード例 #9
0
 public DictionaryBinder(ISafeSettingsBinder <TKey> keyBinder, ISafeSettingsBinder <TValue> valueBinder)
 {
     this.keyBinder   = keyBinder;
     this.valueBinder = valueBinder;
 }
コード例 #10
0
 public NullableBinder(ISafeSettingsBinder <T> valueBinder) =>
 this.valueBinder = valueBinder;
コード例 #11
0
 public BinderWrapper(ISafeSettingsBinder <T> binder) => this.binder = binder;
コード例 #12
0
 public static SettingsBindingResult <TSettings> BindOrDefault <TSettings>(this ISafeSettingsBinder <TSettings> binder, ISettingsNode node) =>
 node.IsNullValue(binder) ? SettingsBindingResult.Success <TSettings>(default) : binder.Bind(node);