Exemplo n.º 1
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
        {
            var identifier = memInfo.AllAttributes<InjectAttribute>().Select(x => x.Identifier)
                .Concat(memInfo.AllAttributes<InjectOptionalAttribute>().Select(x => x.Identifier)).FirstOrDefault();

            Type memberType;
            Action<object, object> setter;

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                memberType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                memberType = propInfo.PropertyType;
            }

            return new InjectableInfo(
                memInfo.HasAttribute(typeof(InjectOptionalAttribute)),
                identifier,
                memInfo.Name,
                memberType,
                parentType,
                setter);
        }
Exemplo n.º 2
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
        {
            var injectAttributes = memInfo.AllAttributes<InjectAttribute>().ToList();
            var injectOptionalAttributes = memInfo.AllAttributes<InjectOptionalAttribute>().ToList();

            string identifier = null;

            Assert.That(injectAttributes.IsEmpty() || injectOptionalAttributes.IsEmpty(),
                "Found both 'InjectOptional' and 'Inject' attributes on type field '{0}' of type '{1}'.  Field should only have one or the other.", memInfo.Name, parentType.Name());

            if (injectAttributes.Any())
            {
                identifier = injectAttributes.Single().Identifier;
            }
            else if (injectOptionalAttributes.Any())
            {
                identifier = injectOptionalAttributes.Single().Identifier;
            }

            Type memberType;
            Action<object, object> setter;

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                memberType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                memberType = propInfo.PropertyType;
            }

            return new InjectableInfo(
                injectOptionalAttributes.Any(),
                identifier,
                memInfo.Name,
                memberType,
                parentType,
                setter,
                null);
        }
Exemplo n.º 3
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type parentType)
        {
            var injectAttributes = memInfo.AllAttributes<InjectAttributeBase>().ToList();

            Assert.That(injectAttributes.Count <= 1,
                "Found multiple 'Inject' attributes on type field '{0}' of type '{1}'.  Field should only container one Inject attribute", memInfo.Name, parentType.Name());

            var injectAttr = injectAttributes.SingleOrDefault();

            string identifier = null;
            bool isOptional = false;
            bool localOnly = false;

            if (injectAttr != null)
            {
                identifier = injectAttr.Identifier;
                isOptional = injectAttr.IsOptional;
                localOnly = injectAttr.LocalOnly;
            }

            Type memberType;
            Action<object, object> setter;

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                memberType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                memberType = propInfo.PropertyType;
            }

            return new InjectableInfo(
                isOptional,
                identifier,
                memInfo.Name,
                memberType,
                parentType,
                setter,
                null,
                localOnly);
        }