Определить, отсутствует ли в десериализованном объекте поле с классом JsonConvert в Json.NET

Недавно мне пришлось написать что-то, что выполнило это на работе, поэтому я решил опубликовать решение этой проблемы. В качестве дополнительного бонуса функциональность этого решения позволяет разделить строку в противоположном направлении и правильно обрабатывать символы юникода, как ранее упоминалось Марвином Пинто выше. Итак, вот оно:

using System;
using Extensions;

namespace TestCSharp
{
    class Program
    {
        static void Main(string[] args)
        {    
            string asciiStr = "This is a string.";
            string unicodeStr = "これは文字列です。";

            string[] array1 = asciiStr.Split(4);
            string[] array2 = asciiStr.Split(-4);

            string[] array3 = asciiStr.Split(7);
            string[] array4 = asciiStr.Split(-7);

            string[] array5 = unicodeStr.Split(5);
            string[] array6 = unicodeStr.Split(-5);
        }
    }
}

namespace Extensions
{
    public static class StringExtensions
    {
        /// Returns a string array that contains the substrings in this string that are seperated a given fixed length.
        /// This string object.
        /// Size of each substring.
        ///     CASE: length > 0 , RESULT: String is split from left to right.
        ///     CASE: length == 0 , RESULT: String is returned as the only entry in the array.
        ///     CASE: length < 0 , RESULT: String is split from right to left.
        /// 
        /// String array that has been split into substrings of equal length.
        /// 
        ///     
        ///         string s = "1234567890";
        ///         string[] a = s.Split(4); // a == { "1234", "5678", "90" }
        ///     
        ///             
        public static string[] Split(this string s, int length)
        {
            System.Globalization.StringInfo str = new System.Globalization.StringInfo(s);

            int lengthAbs = Math.Abs(length);

            if (str == null || str.LengthInTextElements == 0 || lengthAbs == 0 || str.LengthInTextElements <= lengthAbs)
                return new string[] { str.ToString() };

            string[] array = new string[(str.LengthInTextElements % lengthAbs == 0 ? str.LengthInTextElements / lengthAbs: (str.LengthInTextElements / lengthAbs) + 1)];

            if (length > 0)
                for (int iStr = 0, iArray = 0; iStr < str.LengthInTextElements && iArray < array.Length; iStr += lengthAbs, iArray++)
                    array[iArray] = str.SubstringByTextElements(iStr, (str.LengthInTextElements - iStr < lengthAbs ? str.LengthInTextElements - iStr : lengthAbs));
            else // if (length < 0)
                for (int iStr = str.LengthInTextElements - 1, iArray = array.Length - 1; iStr >= 0 && iArray >= 0; iStr -= lengthAbs, iArray--)
                    array[iArray] = str.SubstringByTextElements((iStr - lengthAbs < 0 ? 0 : iStr - lengthAbs + 1), (iStr - lengthAbs < 0 ? iStr + 1 : lengthAbs));

            return array;
        }
    }
}

Также здесь приведена ссылка на результаты запуска этого кода: http://i.imgur.com/16Iih.png

37
задан wonea 4 February 2019 в 21:36
поделиться