public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { object _returnVal; if (!_parseEnumAsArray && typeToParse.GetCustomAttribute <FlagsAttribute>() != null) { _parseEnumAsArray = true; Type _enumArrayType = typeToParse.MakeArrayType(); ITypeParser _arrayParser = parserContainer.GetParser(_enumArrayType); Array _enumArray = _arrayParser.Parse(toParse, _enumArrayType, parserContainer) as Array; int _totalVal = 0; int _iter = 0; foreach (object enVal in _enumArray) { if (_iter == 0) { _totalVal = (int)enVal; } else { _totalVal = _totalVal | (int)enVal; } _iter++; } _returnVal = _totalVal; _parseEnumAsArray = false; } else { _returnVal = Enum.Parse(typeToParse, toParse, true); } return(_returnVal); }
public object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { Type _myUnderLyingType = Nullable.GetUnderlyingType(typeToParse); object _rtrnVal = parserContainer.GetParser(_myUnderLyingType)?.Parse(toParse, _myUnderLyingType, parserContainer); return(_rtrnVal); }
public SwitchParser(ITypeParserContainer typeParser, ParamsObject paramsObject) { _typeParser = typeParser; _paramsObject = paramsObject; GetSwitchOptions(); GetAvailableSwitchProperties(); }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { IEnumerable <Type> _types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes()); Type _returnType = _types.SkipWhile(t => (t.GetCustomAttribute <TypeParamAttribute>() != null && t.GetCustomAttribute <TypeParamAttribute>().Ignore)) .Where(t => t.MatchesAttributeValueOrName <TypeParamAttribute>(toParse, attr => (attr == null || string.IsNullOrWhiteSpace(attr.FriendlyName)) ? "" : attr.FriendlyName.ToLower())).FirstOrDefault(); return(_returnType); }
private object ParseElement(string toParse, Type type, ITypeParserContainer parserContainer) { ITypeParser _parser = parserContainer.GetParser(type); if (_parser == null) { throw new Exception($"Parsing type '{type.Name}' not handled"); } return(_parser.Parse(toParse, type, parserContainer)); }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { var secure = new System.Security.SecureString(); foreach (var v in toParse.ToCharArray()) { secure.AppendChar(v); } return(secure); }
private void Initialize() { _defaultSwitchOptions = new SwitchOptions(new List <char> { '/' }, new List <char> { ':' }, "[_A-Za-z]+[_A-Za-z0-9]*"); _defaultTypeParserContainer = new DefaultTypeContainer(); _defaultSwitchParser = new SwitchParser(TypeParser, this); _helpOptions = new HelpTextOptions(HelpTextLength, HelpTextIndentLength, HelpCommands); _defaultHelpTextParser = new BasicHelpTextParser(_helpOptions, TypeParser); _defaultArgMaker = new DefaultArgumentCreator(); }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { if (BoolFalseValues.Contains(toParse?.ToLower().Trim())) { return(false); } else if (BoolTrueValues.Contains(toParse?.ToLower().Trim())) { return(true); } else { throw new Exception("Boolean value invalid"); } }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { Type elementType = typeToParse.GetElementType(); //Comma delimited, or comma+space delimited string[] splits = toParse.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); splits = splits.Select(s => s.Trim()).ToArray(); Type[] myTypes = new Type[] { typeof(int) }; object[] myReqs = new object[] { splits.Length }; Array returnVals = typeToParse.GetConstructor(myTypes).Invoke(myReqs) as Array; //object[] returnVals = new object[splits.Length]; for (int i = 0; i < splits.Length; i++) { string split = splits[i]; object myElement = ParseElement(split, elementType, parserContainer); returnVals.SetValue(myElement, i); } return(returnVals); }
public void TestOverrideVirtualComplexProperty_ParamsObject() { //create and save new type object _newProduct = DynamicTypeCreator .Create <ParamsObject>("NewParams") .AddPassThroughCtors() .OverrideGet <ITypeParserContainer>("TypeParser", () => new TypeParserContainer(false, new KeyValueParser())) .FinishBuildingType() .GetConstructor(new Type[] { typeof(string[]) }) .Invoke(new object[] { new string[0] { } }); PropertyInfo _meth = _newProduct.GetType().GetProperties().FirstOrDefault(m => m.Name == "TypeParser"); Assert.IsNotNull(_meth, "new method not found"); ITypeParserContainer _yis = (ITypeParserContainer)_meth.GetValue(_newProduct); bool _stringNotAccepted = false; try { _yis.GetParser(typeof(string)); } catch { _stringNotAccepted = true; } Assert.IsTrue(_stringNotAccepted, "String parser found"); bool _keyValueParserFound = true; try { _yis.GetParser(typeof(KeyValuePair <,>)); } catch { _keyValueParserFound = false; } Assert.IsTrue(_keyValueParserFound, "Keyvalue parser not found"); }
public object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { Type[] _genericTypes = typeToParse.GetGenericArguments(); string[] _keyValArr = toParse.Split(':'); if (_keyValArr == null || _keyValArr.Length < 1 || _keyValArr.Length > 2) { throw new Exception("Failed to parse key value pair"); } object _key = ParseElement(_keyValArr[0], _genericTypes[0], parserContainer); object _val = null; if (_keyValArr.Length > 1) { _val = ParseElement(_keyValArr[1], _genericTypes[1], parserContainer); } if (_key == null) { throw new Exception("Failed to parse key value pair"); } object[] _keyValPair = new object[] { _key, _val }; object myVal = typeToParse.GetConstructor(_genericTypes).Invoke(_keyValPair); return(myVal); }
public BasicHelpTextParser(HelpTextOptions options, ITypeParserContainer parser) { _options = options; _parser = parser; }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { CultureInfo _culture = CultureInfo.CreateSpecificCulture(""); return(DateTime.ParseExact(toParse, "dd-MM-yyyy", _culture)); }
public override object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer) { return(Convert.ChangeType(toParse, typeToParse)); }
public abstract object Parse(string toParse, Type typeToParse, ITypeParserContainer parserContainer);