コード例 #1
0
        public void SetValue(object obj)
        {
            if (obj?.GetType() != ArgumentType)
            {
            }                                       // TODO: Throw exception if different types

            ICatagoryInstance instance = _currentCatagory as ICatagoryInstance;

            // If the argument is a flags enum we can't just set the value we need to perform a bitwise or on it.
            // Because the enum type is unknown at compile time we need to go about it the long way by casting the
            // enum to it's base value and setting it. To bitwise or we need to get it into an integer like type.
            if (IsFlags)
            {
                // Cast to long to be safe as this should cover 99.99% of enums.
                // Numbers > long.MaxValue will throw an exception but that is such a fringe it's not worth the effort.
                long bitwiseResult = Convert.ToInt64(GetValue()) | Convert.ToInt64(obj);

                // Get the enums underlying type to convert the result value to the actual value that will be set
                // You don't need the specifc enum type as the underlying type can implicitly be set
                Type underlyingType = Enum.GetUnderlyingType(ArgumentType);
                Property.SetValue(instance.CatagoryInstance, Convert.ChangeType(bitwiseResult, underlyingType));
            }
            else
            {
                Property.SetValue(instance.CatagoryInstance, obj);
            }

            ValueSet = true;
        }
コード例 #2
0
ファイル: Command.cs プロジェクト: watermelonpizza/argparse
        public void SetValue(object obj)
        {
            if (obj?.GetType() != Property.PropertyType)
            {
            }                                                // TODO: Throw exception if different types

            ICatagoryInstance instance = _currentCatagory as ICatagoryInstance;

            Property.SetValue(instance.CatagoryInstance, obj);

            ValueSet = true;
        }
コード例 #3
0
        public void AddValue(object obj)
        {
            if (obj?.GetType() != typeof(TArgument))
            {
            }                                            // TODO: Throw exception if different types

            if (IsMultiple)
            {
                try
                {
                    ICatagoryInstance instance = _currentCatagory as ICatagoryInstance;

                    // If the property is an ImmutableArray
                    if (Property.GetValue(instance.CatagoryInstance) is ImmutableArray <TArgument> propValue)
                    {
                        // If it's not default, add the new value and set it back
                        if (propValue.IsDefault)
                        {
                            Property.SetValue(
                                instance.CatagoryInstance,
                                ImmutableArray.Create((TArgument)obj));

                            ValueSet = true;
                        }
                        // Otherwise create a new list and set the property
                        else
                        {
                            ImmutableArray <TArgument> newPropertyArray = propValue.Add((TArgument)obj);
                            Property.SetValue(instance.CatagoryInstance, newPropertyArray);

                            ValueSet = true;
                        }
                    }
                    else
                    {
                        // TODO: Can't be not ImmutableArray<TArgument> (can't get here?)
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                throw new Exception($"Tried to add a value to the MultiParamter '{ParameterName}' but the IsMultiple flag wasn't set.");
            }
        }
コード例 #4
0
ファイル: Command.cs プロジェクト: watermelonpizza/argparse
        public object GetValue()
        {
            ICatagoryInstance instance = _currentCatagory as ICatagoryInstance;

            return(Property.GetValue(instance.CatagoryInstance));
        }