private static void ExtractProperties <K>(IContextContainer <K> context, IContextObject extractObj)
        {
            List <PropertyInfo> properties = cachedTypePropertyDic[extractObj.GetType()][ContextUsage.Extract];

            if (properties != null && properties.Count > 0)
            {
                foreach (var property in properties)
                {
                    if (typeof(IContextContainer <>).IsAssignableFrom(property.PropertyType))
                    {
                        throw new InvalidOperationException("Context can only be used with the InjectUsage.In option.");
                    }

                    var    attr       = property.GetCustomAttribute <ContextIEAttribute>();
                    object fieldValue = property.GetValue(extractObj);
                    if (!attr.Optional)
                    {
                        context.AddOrUpdate((K)attr.Key, fieldValue);
                    }
                    else if (fieldValue != null)
                    {
                        context.AddOrUpdate((K)attr.Key, fieldValue);
                    }
                }
            }
        }
        private static void ExtractFields <K>(IContextContainer <K> context, IContextObject extractObj)
        {
            List <FieldInfo> fields = cachedTypeFieldDic[extractObj.GetType()][ContextUsage.Extract];

            if (fields != null && fields.Count > 0)
            {
                foreach (var field in fields)
                {
                    if (typeof(IContextContainer <>).IsAssignableFrom(field.FieldType))
                    {
                        throw new InvalidOperationException("Context can only be used with the InjectUsage.In option.");
                    }

                    var    attr  = field.GetCustomAttribute <ContextIEAttribute>();
                    object value = field.GetValue(extractObj);
                    if (!attr.Optional)
                    {
                        context.AddOrUpdate((K)attr.Key, value);
                    }
                    else if (value != null)
                    {
                        context.AddOrUpdate((K)attr.Key, value);
                    }
                }
            }
        }
        private IEnumerable <Type> WalkAssignableTypes(IContextObject contextObject)
        {
            var iCType = typeof(IContextObject);

            foreach (Type t in contextObject.GetType().GetInterfaces())
            {
                if (iCType.IsAssignableFrom(t) && t != iCType)
                {
                    yield return(t);
                }
            }

            for (var current = contextObject.GetType(); current != null; current = current.BaseType)
            {
                if (iCType.IsAssignableFrom(current) && current != iCType)
                {
                    yield return(current);
                }
            }
        }
示例#4
0
        /// <summary>
        /// 设置情景对象
        /// </summary>
        public void SetContextObject(IContextObject contextObject)
        {
            if (contextObject == null)
            {
                throw new ArgumentNullException("contextObject");
            }

            var type = contextObject.GetType();

            if (_contextObjects.ContainsKey(type))
            {
                throw new Exception($"Context object {type} is already existed.");
            }

            _contextObjects.Add(type, contextObject);
        }
示例#5
0
        /// <inheritdoc />
        public void SetContextObject(IContextObject contextObject)
        {
            if (contextObject == null)
            {
                throw new ArgumentNullException("contextObject");
            }

            var iCType = typeof(IContextObject);

            Type[] iTypes = contextObject.GetType().GetInterfaces();
            foreach (var iType in iTypes)
            {
                if (!iCType.IsAssignableFrom(iType) || iType == iCType)
                {
                    continue;
                }
                m_ContextObjects[iType] = contextObject;
            }
        }
        private static void InjectProperties <K>(IContextContainer <K> context, IContextObject injectObj)
        {
            List <PropertyInfo> properties = cachedTypePropertyDic[injectObj.GetType()][ContextUsage.Inject];

            if (properties != null && properties.Count > 0)
            {
                foreach (var property in properties)
                {
                    var attr = property.GetCustomAttribute <ContextIEAttribute>();

                    if (!context.TryGet((K)attr.Key, out object value))
                    {
                        if (value == null && !attr.Optional)
                        {
                            throw new ContextValueNullException(attr.Key);
                        }
                    }
                    property.SetValue(injectObj, value);
                }
            }
        }
        private static void InjectFields <K>(IContextContainer <K> context, IContextObject injectObj)
        {
            List <FieldInfo> fields = cachedTypeFieldDic[injectObj.GetType()][ContextUsage.Inject];

            if (fields != null && fields.Count > 0)
            {
                foreach (var field in fields)
                {
                    var attr = field.GetCustomAttribute <ContextIEAttribute>();

                    if (!context.TryGet((K)attr.Key, out object value))
                    {
                        if (value == null && !attr.Optional)
                        {
                            throw new ContextValueNullException(attr.Key);
                        }
                    }
                    field.SetValue(injectObj, value);
                }
            }
        }
        public static void Inject <K>(IContextContainer <K> context, IContextObject injectObj)
        {
            if (injectObj == null || context == null)
            {
                throw new ArgumentNullException("argument is null.");
            }
            Type injectObjType = injectObj.GetType();

            if (!cachedTypeFieldDic.ContainsKey(injectObjType))
            {
                CachedFields(injectObjType);
            }

            if (!cachedTypePropertyDic.ContainsKey(injectObjType))
            {
                CachedProperties(injectObjType);
            }

            InjectFields(context, injectObj);
            InjectProperties(context, injectObj);
        }
        public static void Extract <K>(IContextContainer <K> context, IContextObject extractObj)
        {
            if (extractObj == null || context == null)
            {
                throw new ArgumentNullException("ContextUtil::Extract->argument is null");
            }

            Type extractObjType = extractObj.GetType();

            if (!cachedTypeFieldDic.ContainsKey(extractObjType))
            {
                CachedFields(extractObjType);
            }

            if (!cachedTypePropertyDic.ContainsKey(extractObjType))
            {
                CachedProperties(extractObjType);
            }

            ExtractFields(context, extractObj);
            ExtractProperties(context, extractObj);
        }
示例#10
0
        /// <inheritdoc />
        public void SetContextObject(Type type, IContextObject contextObject)
        {
            if (contextObject == null)
            {
                throw new ArgumentNullException("contextObject");
            }

            if (!type.IsInterface)
            {
                throw new InvalidOperationException(string.Format("Passed in type '{0}' is not an interface.", type));
            }
            if (!type.IsInstanceOfType(contextObject))
            {
                throw new InvalidOperationException(string.Format("'{0}' is not of passed in type '{1}'.", contextObject.GetType(), type));
            }
            m_ContextObjects[type] = contextObject;
        }
示例#11
0
        /// <inheritdoc />
        public void SetContextObject <T>(IContextObject contextObject) where T : IContextObject
        {
            if (contextObject == null)
            {
                throw new ArgumentNullException("contextObject");
            }

            var type = typeof(T);

            if (!type.IsInterface)
            {
                throw new InvalidOperationException(string.Format("Passed in type '{0}' is not an interface.", type));
            }
            if (!(contextObject is T))
            {
                throw new InvalidOperationException(string.Format("'{0}' is not of passed in type '{1}'.", contextObject.GetType(), type));
            }
            m_ContextObjects[typeof(T)] = contextObject;
        }
示例#12
0
        /// <inheritdoc />
        public void SetContextObject(IContextObject contextObject)
        {
            if (contextObject == null)
            {
                throw new ArgumentNullException("contextObject");
            }

            List <Type> types = new List <Type>(WalkAssignableTypes(contextObject));

            if (types.Count == 0)
            {
                throw new Exception($"Could not determine context object type for object of type {contextObject.GetType().FullName}");
            }
            types.ForEach(x => m_ContextObjects[x] = contextObject);
        }