示例#1
0
        internal static MessageDefinition GetDefinitionForType(Type type)
        {
            // cache
            if (Definitions.TryGetValue(type, out var res))
            {
                return(res);
            }

            // if it is a message
            if (!AttributeCache <MessageAttribute> .TryHasAttribute(type, out var msgAttrib))
            {
                res = default;
                Definitions.TryAdd(type, res);
                return(res);
            }

            var repeatable = AttributeCache <RepeatableAttribute> .TryHasAttribute(type, out var _);

            // store properties
            var props = type.GetProperties();

            var max      = 0;
            var msgProps = new MessageProperty[props.Length];

            for (var j = 0; j < props.Length; j++)
            {
                var i = props[j];

                if (HandleItem(i, out var prop))
                {
                    msgProps[max++] = prop;
                }
            }

            // resize the array if needed
            if (msgProps.Length != max)
            {
                var newMsgProps = new MessageProperty[max];
                Array.Copy(msgProps, 0, newMsgProps, 0, max);
                msgProps = newMsgProps;
            }

            var msgDef = new MessageDefinition(
                msgAttrib[0].Type,
                msgProps,
                repeatable
                );

            Definitions.TryAdd(type, msgDef);
            return(msgDef);
        }
示例#2
0
        static MethodInvoker()
        {
            Cache = new HashcodeDictionary <Type, MethodInfo[]>();

            MethodInfoCache = new HashcodeDictionary <MethodInfo, Action <object, object> >();

            //TODO: clean
            // with like a reflection helper class
            // or something

            var dict = new Dictionary <Type, List <MethodInfo> >();

            foreach (var i in _tclassType.GetMethods().Where(x => AttributeCache <DeserializedHandlerAttribute> .HasAttribute(x)))
            {
                if (dict.TryGetValue(i.GetParameters()[0].ParameterType, out var val))
                {
                    val.Add(i);
                }
                else
                {
                    dict[i.GetParameters()[0].ParameterType] = new List <MethodInfo> {
                        i
                    }
                }
            }
            ;

            foreach (var i in dict)
            {
                Cache.TryAdd(i.Key, i.Value.ToArray());
            }
        }
示例#3
0
        public static void InvokeMethod <TItem>(MethodInfo method, object instance, TItem item)
        {
            if (!MethodInfoCache.TryGetValue(method, out var invoke))
            {
                invoke = method
                         .GetSingleInvokable();

                MethodInfoCache.TryAdd(method, invoke);
            }

            invoke(instance, item);
        }