Inheritance: System.Attribute
Ejemplo n.º 1
0
        public ComponentDescriptor(Type type, ComponentAttribute componentAttribute)
        {
            this.Type = type;
            this.Attributes = componentAttribute;
            this.Injections = new List<InjectDescriptor>();

            foreach (MemberInfo member in type.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                if (member is FieldInfo || member is PropertyInfo)
                {
                    InjectAttribute injectAttribute = null;

                    if ((injectAttribute = member.GetCustomAttribute<InjectAttribute>()) != null)
                    {
                        Injections.Add(new InjectDescriptor(member, injectAttribute));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public ComponentDescriptor(Type type, ComponentAttribute componentAttribute)
        {
            this.Type       = type;
            this.Attributes = componentAttribute;
            this.Injections = new List <InjectDescriptor>();

            foreach (MemberInfo member in type.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                if (member is FieldInfo || member is PropertyInfo)
                {
                    InjectAttribute injectAttribute = null;

                    if ((injectAttribute = member.GetCustomAttribute <InjectAttribute>()) != null)
                    {
                        Injections.Add(new InjectDescriptor(member, injectAttribute));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Registers a component by type with this context. Note: You can only call this on a Context that hasn't started.
        /// </summary>
        /// <param name="type"></param>
        public Context Register(Type type)
        {
            AssertState(ContextState.Created);

            if (type == null)
            {
                throw new ArgumentNullException("Type cannot be null.");
            }

            ComponentAttribute componentAttribute = type.GetCustomAttribute <ComponentAttribute>();

            if (componentAttribute == null)
            {
                throw new ArgumentException("Type: '" + type + "' is not marked as a Component.");
            }

            RegisterComponent(type, componentAttribute);

            return(this);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Registers a components of the given type and attributes.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="componentAttribute"></param>
        private void RegisterComponent(Type type, ComponentAttribute componentAttribute)
        {
            lock (_contextMutex)
            {
                AssertState(ContextState.Created);

                if (componentAttribute.Name == null)
                {
                    componentAttribute.Name = type.Name;
                }

                ComponentDescriptor existingDescriptor = null;

                if (descriptorNameCache.TryGetValue(componentAttribute.Name, out existingDescriptor))
                {
                    ComponentDescriptor descriptor = descriptorNameCache[componentAttribute.Name];

                    if (!existingDescriptor.Type.AssemblyQualifiedName.Equals(type.AssemblyQualifiedName))
                    {
                        throw new ComponentNameException("Component name '" + componentAttribute.Name + "' is already used by '" + descriptor.Type + "'");
                    }
                    else
                    {
                        return;
                    }
                }

                string typeName = type.AssemblyQualifiedName;

                if (descriptorTypeCache.ContainsKey(typeName))
                {
                    return;
                }

                ComponentDescriptor componentDescriptor = new ComponentDescriptor(type, componentAttribute);

                descriptorTypeCache[typeName] = componentDescriptor;
                descriptorNameCache[componentAttribute.Name] = componentDescriptor;
            }
        }