Exemplo n.º 1
0
        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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        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);
                    }
                }
            }
        }