Example #1
0
        public static bool TryConvertTo(ISsslConverter converter, SsslObject sssl, Type type, ObjectConversionOptions options, [NotNullWhen(true)] out object result)
        {
            var allowUnknownMember = options.HasFlag(ObjectConversionOptions.AllowUnknownMember);
            var allowMissingMember = options.HasFlag(ObjectConversionOptions.AllowMissingMember);

            result = null !;

            if (!(sssl is SsslRecord record))
            {
                return(false);
            }

            if (record.Name != "" && !type.IsSealed && record.Name != type.GetFullName())
            {
                if (type.Assembly.GetType(record.Name) is { } assemblyInnerType&&
                    type.IsAssignableFrom(assemblyInnerType))
                {
                    type = assemblyInnerType;
                }
                if (Type.GetType(record.Name) is { } coreType&&
                    type.IsAssignableFrom(coreType))
                {
                    type = coreType;
                }
                else if (AppDomain.CurrentDomain
                         .GetAssemblies()
                         .Select(x => x.GetType(record.Name))
                         .FirstOrDefault(x => x is { } && type.IsAssignableFrom(x)) is { } otherType&&
Example #2
0
        public static T ConvertTo <T>(this ISsslConverter converter, SsslObject ssslObject)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            return((T)converter.ConvertTo(ssslObject, typeof(T)) !);
        }
Example #3
0
        public static object?ConvertTo(this ISsslConverter converter, SsslObject ssslObject, Type type)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            return(converter.TryConvertTo(ssslObject, type, out var result)
                ? result
                : throw new InvalidCastException());
        }
Example #4
0
        public static SsslObject ConvertFrom(this ISsslConverter converter, object?value)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            return(converter.TryConvertFrom(value, out var result)
                ? result
                : throw new InvalidCastException());
        }
Example #5
0
        public static bool TryConvertTo <T>(this ISsslConverter converter, SsslObject ssslObject, [MaybeNull] out T result)
        {
            if (converter is null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            if (converter.TryConvertTo(ssslObject, typeof(T), out var obj))
            {
                result = (T)obj !;
                return(true);
            }

            result = default !;
Example #6
0
        public static bool TryConvertFrom(ISsslConverter converter, object value, [NotNullWhen(true)] out SsslObject result)
        {
            var type   = value.GetType();
            var obj    = new SsslRecord(SsslRecordType.Braces, type.GetFullName());
            var values = GetModelInfo(type).ConvertFrom(value);

            foreach (var(name, val) in values)
            {
                if (!converter.TryConvertFrom(val, out var ssslVal))
                {
                    result = null !;
                    return(false);
                }

                obj[name] = ssslVal;
            }

            result = obj;
            return(true);
        }
Example #7
0
 public dynamic ToDynamic(ISsslConverter converter) => new SsslDynamicProvider(this, converter);
Example #8
0
 internal SsslDynamicProvider(SsslObject ssslObject, ISsslConverter converter)
 {
     _ssslObject = ssslObject;
     _converter  = converter;
 }
Example #9
0
 internal SsslDynamicProvider(SsslObject ssslObject)
 {
     _ssslObject = ssslObject;
     _converter  = SsslConverter.Default;
 }