Exemple #1
0
        public static object ConvertTo(this object from, Type type)
        {
            if (from == null)
            {
                return(null);
            }

            if (from.GetType() == type)
            {
                return(from);
            }

            if (from.GetType().IsValueType || type.IsValueType)
            {
                return(ChangeValueType(from, type));
            }

            if (from is string str)
            {
                return(TypeSerializer.DeserializeFromString(str, type));
            }
            if (from is ReadOnlyMemory <char> rom)
            {
                return(TypeSerializer.DeserializeFromSpan(type, rom.Span));
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                var listResult = TranslateListWithElements.TryTranslateCollections(
                    from.GetType(), type, from);

                return(listResult);
            }

            var to = type.CreateInstance();

            return(to.PopulateWithNonDefaultValues(from));
        }
Exemple #2
0
 public static T FromJsvSpan <T>(this ReadOnlySpan <char> jsv)
 {
     return(TypeSerializer.DeserializeFromSpan <T>(jsv));
 }