Esempio n. 1
0
    // --------------------------------------------------------------------
    /// <summary>
    /// Create an instance of a the 'Instance' class based on the
    /// Node type passed in information. Then populate the instance based
    /// on the child XML nodes.
    /// </summary>
    /// <param name="Node"></param>
    /// <param name="Parent"></param>
    /// <param name="ParentInstance"></param>
    /// <param name="HostComponent"></param>
    /// <returns></returns>
    // --------------------------------------------------------------------
    private Instance CreateInstance(XmlNode Node,
                                    XmlNode Parent,
                                    Instance ParentInstance,
                                    ApsimComponent ParentComponent)
    {
        Type ClassType = GetTypeOfChild(Node, ParentInstance);

        if (ClassType == null)
        {
            throw new Exception("Cannot find a class called: " + Node.Name);
        }
        object   Model = Activator.CreateInstance(ClassType);
        Instance CreatedInstance;

        if (Model.GetType().IsSubclassOf(typeof(Instance)))
        {
            CreatedInstance = (Instance)Model;
        }
        else
        {
            CreatedInstance = new Instance(Model);
        }

        //if (CreatedInstance.GetType().IsSubclassOf(typeof(DerivedInstance)))
        //{
        //    ((DerivedInstance)CreatedInstance).Initialise(Node, ParentInstance, ParentComponent);
        //}
        if (CreatedInstance != null)
        {
            CreatedInstance.Initialise(XmlHelper.Name(Node), ParentInstance, ParentComponent);
            GetAllProperties(CreatedInstance, Parent);
            GetAllEventHandlers(CreatedInstance);
            GetAllEvents(CreatedInstance);
            PopulateParams(CreatedInstance, Node, ParentComponent);
        }
        else
        {
            throw new Exception("Class " + Node.Name + " must be derived from the \"Instance\" class");
        }

        return(CreatedInstance);
    }