예제 #1
0
 protected virtual void ParseConstructors(Type type, MemberKind kind, DescriptionProvider provider)
 {
     if ((kind & MemberKind.Constructor) == MemberKind.Constructor)
     {
         ConstructorInfo[] constructors        = type.GetConstructors();
         ConstructorInfo   constructorToInject = null;
         int maxParameters = -1;
         foreach (ConstructorInfo constructor in constructors)
         {
             object[] attributes = constructor.GetCustomAttributes(true);
             var      attrLen    = attributes.Length;
             for (int i = 0; i < attrLen; i++)
             {
                 var  attribute = attributes[i] as Attribute;
                 Type attributeType;
                 if (attribute != null && provider.IsMappedAttribute(attributeType = attribute.GetType()))
                 {
                     AddDescription(attributeType, new ConstructorDescription(constructor, attribute));
                 }
             }
             if (constructor.GetParameters().Length > maxParameters)
             {
                 constructorToInject = constructor;
                 maxParameters       = constructor.GetParameters().Length;
             }
         }
         if (constructorToInject != null)
         {
             DefaultConstructor = new ConstructorDescription(constructorToInject, null);
         }
     }
 }
예제 #2
0
        private object CreateInstance(IInjector injector, Type type)
        {
            var provider     = injector.DescriptionProvider.GetProvider(type);
            var constructors = provider.GetByAttribute <InjectAttribute>();
            ConstructorDescription constructor = null;

            if (constructors != null)
            {
                constructor = constructors.FirstOrDefault(m => m.Kind == MemberKind.Constructor) as ConstructorDescription;
            }
            if (constructor == null)
            {
                constructor = provider.DefaultConstructor;
            }
            if (constructor != null)
            {
                return(constructor.CreateInstance(type, injector));
            }

            var result = Activator.CreateInstance(type);

            return(result);
        }