コード例 #1
0
        private static void TestParse <T>(string stringValue, T expectedValue)
        {
            var result1 = GenericParser.Parse <T>(stringValue);

            Assert.Equal(expectedValue, result1);

            var result2 = GenericParser.Parse(stringValue, typeof(T));

            Assert.Equal(expectedValue, result2);

            var result3 = GenericParser.TryParse(stringValue, out T out1);

            Assert.True(result3);
            Assert.Equal(expectedValue, out1);

            var result4 = GenericParser.TryParse(stringValue, out object out2, typeof(T));

            Assert.True(result4);
            Assert.Equal(expectedValue, out2);

            var result5 = stringValue.Parse <T>();

            Assert.Equal(expectedValue, result5);

            var result6 = stringValue.Parse(typeof(T));

            Assert.Equal(expectedValue, result6);
        }
コード例 #2
0
        public void Test_TryParseFail()
        {
            var res1 = GenericParser.TryParse("foo", out int _);

            Assert.False(res1);

            var res2 = GenericParser.TryParse("bar", out object _, typeof(int));

            Assert.False(res2);
        }
コード例 #3
0
        public void Test_RegisterParser()
        {
            GenericParser.RegisterParser(typeof(object), s => s);

            var res1 = GenericParser.TryParse("foo", out object o1);

            Assert.True(res1);
            Assert.Equal("foo", o1.ToString());

            var res2 = GenericParser.TryParse("bar", out object o2, typeof(object));

            Assert.True(res2);
            Assert.Equal("bar", o2.ToString());
        }
コード例 #4
0
        private static bool GenericTryParse <T>(string s, out T t)
        {
            return(GenericParser <T> .TryParse(s, out t));

            //Helper.CheckCondition(GenericParser<T>.IsParsable(), "Cannot parse type {0}. It does not have a TryParse or Parse method defined", typeof(T));
            //// now the general one.
            //bool success = false;
            //t = default(T);
            ////Type type = typeof(T);

            ////MethodInfo tryParse = type.GetMethod("TryParse", new Type[] { typeof(string), type.MakeByRefType() });

            //if (GenericParser<T>.TryParse != null)
            //{
            //    object[] args = new object[] { s, t };

            //    success = (bool)GenericParser<T>.TryParse.Invoke(null, args);

            //    if (success)
            //    {
            //        t = (T)args[1];
            //    }
            //}
            //else
            //{
            //    //MethodInfo parse = type.GetMethod("Parse", new Type[] { typeof(string) });

            //    try
            //    {
            //        object[] args = new object[] { s };
            //        t = (T)GenericParser<T>.TryParse.Invoke(null, args);
            //        success = true;
            //    }
            //    catch { }
            //}

            //return success;
        }
コード例 #5
0
ファイル: Parser.cs プロジェクト: Szunyike/SAM-Statistic-2018
 /// <summary>
 /// Generic Try Parse.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="s"></param>
 /// <param name="t"></param>
 /// <returns></returns>
 private static bool GenericTryParse <T>(string s, out T t)
 {
     return(GenericParser <T> .TryParse(s, out t));
 }
コード例 #6
0
        //private static bool NullableTryParse<T>(string s, out T t) where T:System.Nullable
        //{
        //	if (string.IsNullOrEmpty(s) || s.Equals("null", StringComparison.CurrentCultureIgnoreCase))
        //	{
        //		return null;
        //	}


        //}

        private static bool GenericTryParse <T>(string s, out T t)
        {
            return(GenericParser <T> .TryParse(s, out t));

            //// now the general one.
            //bool success = false;
            //t = default(T);
            //Type type = typeof(T);

            //MethodInfo tryParse = type.GetMethod("TryParse", new Type[] { typeof(string), type.MakeByRefType() });

            //if (tryParse != null && tryParse.IsStatic)
            //{
            //    object[] args = new object[] { s, t };

            //    success = (bool)tryParse.Invoke(null, args);

            //    if (success)
            //    {
            //        t = (T)args[1];
            //    }
            //}
            //else
            //{
            //    MethodInfo parse = type.GetMethod("Parse", new Type[] { typeof(string) });
            //    if (parse != null && parse.IsStatic)
            //    {
            //        Helper.CheckCondition<ParseException>(parse != null, "Cannot parse type {0}. It does not have a TryParse or Parse method defined", typeof(T).ToTypeString());

            //        try
            //        {
            //            object[] args = new object[] { s };
            //            t = (T)parse.Invoke(null, args);
            //            success = true;
            //        }
            //        catch (TargetInvocationException e)
            //        {
            //            if (e.InnerException is HelpException || e.InnerException is ParseException)
            //                throw e.InnerException;
            //        }
            //    }
            //    else //if (type.IsParsable() || type.IsInterface || type.IsAbstract)
            //    {
            //        ConstructorArguments constLine = new ConstructorArguments(s);
            //        try
            //        {
            //            t = constLine.Construct<T>();
            //            success = true;
            //        }
            //        catch (HelpException)
            //        {
            //            throw;
            //        }
            //        catch (ParseException)
            //        {
            //            throw;
            //        }
            //    }
            //    //else
            //    //{
            //    //    throw new ParseException("Cannot parse type {0}. It does not have a TryParse or Parse method defined, nor does it have a public default constructor, nor is it an interface or abstract type.", typeof(T).ToTypeString());
            //    //}
            //}

            //return success;
        }