private static void InjectProperties <K>(IEnvContext <K> context, Type injectObjType, object injectObj) { List <PropertyInfo> properties = cachedTypePropertyDic[injectObjType][ContextUsage.Out]; if (properties != null && properties.Count > 0) { foreach (var property in properties) { var attr = property.GetCustomAttribute <ContextFieldAttribute>(); if (!context.TryGet((K)attr.Key, out object fieldValue)) { if (fieldValue == null && !attr.Optional) { throw new Exception($"ContextUtil::Inject->Data not found.fieldName = {property.Name},key = {attr.Key}"); } } property.SetValue(injectObj, fieldValue); } } }
private static void InjectFields <K>(IEnvContext <K> context, Type injectObjType, object injectObj) { List <FieldInfo> fields = cachedTypeFieldDic[injectObjType][ContextUsage.In]; if (fields != null && fields.Count > 0) { foreach (var field in fields) { var attr = field.GetCustomAttribute <ContextFieldAttribute>(); if (!context.TryGet((K)attr.Key, out object fieldValue)) { if (fieldValue == null && !attr.Optional) { throw new Exception($"ContextUtil::Inject->Data not found.fieldName = {field.Name},key = {attr.Key}"); } } field.SetValue(injectObj, fieldValue); } } }