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); } } }