コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        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);
                }
            }
        }