public void Add(Type type, MyComponentBase component)
        {
            //System.Diagnostics.Debug.Assert(component == null || component.ContainerBase == null, "Component needs to be removed from a container before adding to a new one!");
            System.Diagnostics.Debug.Assert(typeof(MyComponentBase).IsAssignableFrom(type), "Unsupported type of component!");
            if (!typeof(MyComponentBase).IsAssignableFrom(type))
            {
                return;
            }

            if (component != null)
            {
                System.Diagnostics.Debug.Assert(type.IsAssignableFrom(component.GetType()), "Component added with wrong type!");
                if (!type.IsAssignableFrom(component.GetType()))
                {
                    return;
                }
            }

            {
                //TODO: componentTypeFromAttribute cannot be null when all components has [MyComponentType(typeof(...))] attribute.
                var componentTypeFromAttribute = MyComponentTypeFactory.GetComponentType(type);
                if (componentTypeFromAttribute != null && componentTypeFromAttribute != type)
                {
                    // Failed when component type from attribute is not the same as the given type. Means that [MyComponentType(typeof(...))]
                    // should be specified for the component class (it is probably used for base class now).
                    System.Diagnostics.Debug.Fail("Component: " + component.GetType() + " is set to container as type: " + type + " but type from attribute is: " + componentTypeFromAttribute);
                }
            }

            MyComponentBase containedComponent;

            if (m_components.TryGetValue(type, out containedComponent))
            {
                //System.Diagnostics.Debug.Assert(containedComponent != component, "Adding a component to a container twice!");

                if (containedComponent is IMyComponentAggregate)
                {
                    (containedComponent as IMyComponentAggregate).AddComponent(component);
                    return;
                }
                else if (component is IMyComponentAggregate)
                {
                    Remove(type);
                    (component as IMyComponentAggregate).AddComponent(containedComponent);
                    m_components[type] = component;
                    component.SetContainer(this);
                    OnComponentAdded(type, component);
                    return;
                }
            }

            Remove(type);
            if (component != null)
            {
                m_components[type] = component;
                component.SetContainer(this);
                OnComponentAdded(type, component);
            }
        }
        public void Deserialize(MyObjectBuilder_ComponentContainer builder)
        {
            if (builder == null || builder.Components == null)
            {
                return;
            }

            foreach (var data in builder.Components)
            {
                MyComponentBase instance            = null;
                var             createdInstanceType = MyComponentFactory.GetCreatedInstanceType(data.Component.TypeId);

                // Old component deserialized type.
                var dictType = MyComponentTypeFactory.GetType(data.TypeId);
                // Component type can be set as attribute now
                var dictTypeFromAttr = MyComponentTypeFactory.GetComponentType(createdInstanceType);
                if (dictTypeFromAttr != null)
                {
                    dictType = dictTypeFromAttr;
                }

                bool hasComponent = TryGet(dictType, out instance);
                if (hasComponent)
                {
                    // If component is found then check also type because some components have default instances (MyNullGameLogicComponent)
                    if (createdInstanceType != instance.GetType())
                    {
                        hasComponent = false;
                    }
                }

                if (!hasComponent)
                {
                    instance = MyComponentFactory.CreateInstanceByTypeId(data.Component.TypeId);
                }

                instance.Deserialize(data.Component);

                if (!hasComponent)
                {
                    Add(dictType, instance);
                }
            }
        }