Beispiel #1
0
        private static IReadOnlyDictionary <string, JsonSerializable> _ToDictionary(JsonSerializable context, IEnumerable <JsonSerializable> serializables)
        {
            var dict = new Dictionary <string, JsonSerializable>();

            foreach (var val in serializables)
            {
                if (val == null)
                {
                    continue;
                }

                string key = null;

                if (val is UnknownNode unk)
                {
                    key = unk.Name;
                }
                else
                {
                    key = ExtensionsFactory.Identify(context.GetType(), val.GetType());
                }

                if (key == null)
                {
                    continue;
                }
                dict[key] = val;
            }

            return(dict);
        }
Beispiel #2
0
        private static string _CreateBaseMessage(JsonSerializable target, String message)
        {
            if (target == null)
            {
                return(message);
            }

            var targetTypeInfo = target.GetType().GetTypeInfo();

            var logicalIndexProp = targetTypeInfo.GetProperty("LogicalIndex");

            var logicalIndex = logicalIndexProp != null ? (int)logicalIndexProp.GetValue(target) : -1;

            if (logicalIndex >= 0)
            {
                return($"{targetTypeInfo.Name}[{logicalIndex}] {message}");
            }

            return($"{targetTypeInfo.Name} {message}");
        }
Beispiel #3
0
        public static TPacket FromModel(JsonSerializable <TPacket> model)
        {
            if (_func is null)
            {
                lock (_lock)
                {
                    var modelType   = model.GetType();
                    var cast        = Expression.Convert(Expression.Constant(model), modelType);
                    var constructor = typeof(TPacket).GetConstructor(new[] { modelType });

                    if (constructor is null)
                    {
                        throw new InvalidOperationException($"The packet must have a public constructor that accepts {modelType.Name} as the only argument.");
                    }

                    var @new      = Expression.New(constructor, cast);
                    var parameter = Expression.Parameter(typeof(JsonSerializable <TPacket>));
                    var lambda    = Expression.Lambda <Func <JsonSerializable <TPacket>, TPacket> >(@new, parameter);
                    _func = lambda.Compile();
                }
            }

            return(_func(model));
        }