Exemplo n.º 1
0
        protected InjectAttributeProcessor([NotNull] MemberInfo member, [NotNull] T attribute)
            : base(member, attribute)
        {
            switch (member)
            {
            case FieldInfo field:
                TargetType   = field.FieldType;
                TargetSetter = field.SetValue;

                break;

            case PropertyInfo property:
                TargetType   = property.PropertyType;
                TargetSetter = property.SetValue;

                break;

            default:
                throw new InvalidOperationException($"Unknown member type: {member}.");
            }

            if (TargetType.IsGenericType && TargetType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                DependencyType = TargetType.GetGenericArguments()[0] ?? TargetType;
                Enumerable     = DependencyType != TargetType;
            }
            else
            {
                DependencyType = TargetType;
            }
        }
        protected InjectAttributeProcessor([NotNull] MemberInfo member, [NotNull] T attribute)
            : base(member, attribute)
        {
            switch (member)
            {
            case FieldInfo field:
                TargetType   = field.FieldType;
                TargetSetter = field.SetValue;

                break;

            case PropertyInfo property:
                TargetType = property.PropertyType;

                if (property.CanWrite)
                {
                    TargetSetter = property.SetValue;
                }
                else
                {
                    var writeableProperty = property.DeclaringType?.GetProperty(
                        property.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    if (writeableProperty == null)
                    {
                        throw new InvalidOperationException(
                                  $"Property '{property.DeclaringType?.Name}.{property.Name}' is not writeable.");
                    }

                    TargetSetter = writeableProperty.SetValue;
                }

                break;

            default:
                throw new InvalidOperationException($"Unknown member type: {member}.");
            }

            if (TargetType.IsGenericType && TargetType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                DependencyType = TargetType.GetGenericArguments()[0] ?? TargetType;
                Enumerable     = DependencyType != TargetType;
            }
            else
            {
                DependencyType = TargetType;
            }
        }
Exemplo n.º 3
0
        protected InjectAttributeProcessor(MemberInfo member, T attribute) : base(member, attribute)
        {
            switch (member)
            {
            case FieldInfo field:
                TargetType   = field.FieldType;
                TargetSetter = field.SetValue;

                break;

            case PropertyInfo property:
                TargetType = property.PropertyType;

                if (property.CanWrite)
                {
                    TargetSetter = property.SetValue;
                }
                else
                {
                    var writableProperty = property.DeclaringType?.GetProperty(
                        property.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    if (writableProperty == null)
                    {
                        throw new InvalidOperationException(
                                  $"Property '{property.DeclaringType?.Name}.{property.Name}' is not writable.");
                    }

                    TargetSetter = writableProperty.SetValue;
                }

                break;

            default:
                throw new InvalidOperationException($"Unknown member type: {member}.");
            }

            DependencyType = TargetType;

            if (!TargetType.IsGenericType)
            {
                return;
            }

            var genType = TargetType.GetGenericTypeDefinition();

            if (genType == typeof(Option <>))
            {
                DependencyType = TargetType.GetGenericArguments()[0];

                Debug.Assert(DependencyType != null, "DependencyType != null");

                Optional = true;
            }
            else if (genType == typeof(IEnumerable <>))
            {
                DependencyType = TargetType.GetGenericArguments()[0];

                Debug.Assert(DependencyType != null, "DependencyType != null");

                Enumerable = true;
            }
        }