Create Type


Node Information
Create an instance of any non-static type, or directly get static types.
General Information
• This node has other options, you can view them by clicking on the info button, this also means according to the node you might be able to add more ports than what is already listed.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Instantiate IN RUN MULTIPLE FALSE FALSE
Get OUT Object MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Types
{
    [IsNode("Create Instance")]
    public class CreateType : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Instantiate");
        Port gettype = new Port(PortType.OUT, typeof(object), AcceptsLinks.MULTIPLE, "Get");
        System.Windows.Controls.MenuItem con = new System.Windows.Controls.MenuItem();
        public CreateType()
        {
            Name = "Create Type";
            AddPort(run);
            AddPort(gettype);
            OptionsAvilable = true;
            InfoContextMenu = new System.Windows.Controls.ContextMenu();
            System.Windows.Controls.MenuItem seltype = new System.Windows.Controls.MenuItem();
            seltype.Header = "Type";
            con.Header = "Constructors";
            InfoContextMenu.Items.Add(seltype);
            InfoContextMenu.Items.Add(con);
            Information = "Create an instance of any non-static type, or directly get static types.";
            seltype.Click += seltype_Click;
        }
        void seltype_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenTypeSelectionMenu(false, false);
        }
        public override void OnTypeSelected(Type type)
        {
            ClearMethod();
            Name = GraphCore.GetTypeName(type);
            if (GraphCore.IsTypeStatic(type))
            {
                Name = GraphCore.GetTypeName(type) + " (Static)";
            }
            ReferenceType = type;
            gettype.DataType = type;
            CreateConstructors(type);
        }
        private void ClearMethod()
        {
            ReferenceName = string.Empty;
            List<port> prts = new List<port>();
            foreach (var item in Ports)
            {
                if (item.IsExtra)
                {
                    prts.Add(item);
                }
            }
            foreach (var item in prts)
            {
                RemovePort(item, false);
            }
        }
        public override void OnConstructorSelected(System.Reflection.ConstructorInfo constructorInfo)
        {
            string nm = ReferenceName;
            ClearMethod();
            ReferenceName = nm;
            foreach (var item in constructorInfo.GetParameters())
            {
                Port port = new Port(PortType.IN, item.ParameterType, AcceptsLinks.ONE, item.Name, true);
                AddPort(port);
            }
        }
        private void CreateConstructors(Type type)
        {
            con.Items.Clear();
            foreach (System.Windows.Controls.MenuItem item in GetConstructors(type))
            {
                con.Items.Add(item);
            }
        }
        public override void UpdateResults()
        {
            Variables.Clear();
            ReferenceName = gettype.ID;
            if (ReferenceType != null)
            {
                CreateConstructors(ReferenceType);
                if (!GraphCore.IsTypeStatic(ReferenceType))
                {
                    if (ReferenceType.GetConstructors().Length > 0)
                    {
                        Variables.Add(new Variable(GraphCore.GetFullTypeName(ReferenceType), gettype.ID, GraphCore.GetDefualtConstructorInit(ReferenceType)));
                        gettype.Instructions = gettype.ID;
                    }
                    else
                    {
                        gettype.Instructions = GraphCore.GetFullTypeName(ReferenceType);
                    }
                }
                else
                {
                    gettype.Instructions = GraphCore.GetFullTypeName(ReferenceType);
                }
            }
            if (ReferenceType != null)
            {
                string init = gettype.ID + " = new " + GraphCore.GetFullTypeName(ReferenceType) + "();";
                string par = string.Empty;
                if (Ports.Count > 2)
                {
                    foreach (var port in Ports)
                    {
                        if (port.IsExtra)
                        {
                            par += port.Text + ":" + GetValue(port) + ", ";
                        }
                    }
                    if (par.Length > 2)
                    {
                        par = par.Remove(par.Length - 2, 2);
                    }
                    init = gettype.ID + " = new " + GraphCore.GetFullTypeName(ReferenceType) + "(" + par + ");";
                }
                if (!GraphCore.IsTypeStatic(ReferenceType))
                {
                    run.Instructions = init;
                }
                else
                {
                    run.Instructions = string.Empty;
                }
            }
        }
    }
}

</port></port>