Ejemplo n.º 1
0
        protected static void PopulateTypeInformation(Type type, TypeInformation typeInformation)
        {
            // Implicit Conversions
            var conversions = new Dictionary <string, TypeConversion>();
            var methods     = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

            foreach (var method in methods)
            {
                if (method.Name == "op_implicit")
                {
                    conversions.Add(method.ReturnType.FullName, TypeConversion.Construct(method));
                }
            }
            typeInformation.ImplicitConversions = conversions;

            // Instancing
            object defaultInstance = null;

            if (type.IsValueType)
            {
                try
                {
                    defaultInstance = Activator.CreateInstance(type);
                }
                catch
                {
                }
            }
            else
            {
                var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                if (constructors.Length == 0 || constructors.Any(c => c.GetParameters().Length == 0))
                {
                    try
                    {
                        defaultInstance = Activator.CreateInstance(type);
                    }
                    catch
                    {
                    }
                }
            }

            // Default Value
            if (defaultInstance != null)
            {
                typeInformation.DefaultValue = JToken.FromObject(defaultInstance);
            }
            else
            {
                typeInformation.DefaultValue = JValue.CreateNull();
            }

            // Fields
            // if (typeInformation.DefaultValue.Type == JTokenType.Object)
            // {
            var fieldInfos = new Dictionary <string, FieldInformation>();

            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(OutputSocket))
                {
                    continue;
                }

                if (field.GetCustomAttribute <JsonIgnoreAttribute>() != null)
                {
                    continue;
                }

                if (field.IsPrivate ||
                    field.Name.StartsWith("m_"))
                {
                    continue;
                }

                fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, defaultInstance));
            }
            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (property.PropertyType == typeof(OutputSocket))
                {
                    continue;
                }

                if (property.GetCustomAttribute <JsonIgnoreAttribute>() != null)
                {
                    continue;
                }

                if (property.GetCustomAttribute <CompilerGeneratedAttribute>() != null)
                {
                    continue;
                }

                var getter = property.GetGetMethod();
                var setter = property.GetSetMethod();

                if (getter == null ||
                    getter.IsPrivate ||
                    setter == null ||
                    property.Name.StartsWith("m_"))
                {
                    continue;
                }

                fieldInfos.Add(property.Name, FieldInformation.ConstructFieldInformation(property, defaultInstance));
            }
            typeInformation.Fields = fieldInfos;
        }
Ejemplo n.º 2
0
        public static NodeInformation Construct(Type nodeType)
        {
            var nodeInformation = new NodeInformation();

            var nodeTemplate     = (NodeTemplate)Activator.CreateInstance(nodeType);
            var metadataInstance = (NodeInstanceBase)nodeTemplate.CreateInstance();

            var instanceType = metadataInstance.GetType();

            var singleNodeGraph = new ManifestCaptureGraphInstance(nodeTemplate);

            int inputId            = 0;
            int outputId           = 0;
            var inputSocketFields  = new List <FieldInfo>();
            var outputSocketFields = new List <FieldInfo>();
            var fieldInfos         = new Dictionary <string, FieldInformation>();

            foreach (var field in nodeType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(OutputSocket))
                {
                    field.SetValue(nodeTemplate, new OutputSocket(outputId));
                    outputId++;
                    outputSocketFields.Add(field);
                }
                else
                {
                    if (field.FieldType == typeof(InputSocket))
                    {
                        field.SetValue(nodeTemplate, new InputSocket(inputId));
                        inputId++;
                        inputSocketFields.Add(field);
                    }
                    fieldInfos.Add(field.Name, FieldInformation.ConstructFieldInformation(field, nodeTemplate));
                }
            }
            nodeInformation.Fields = fieldInfos;

            var nodeProperty = instanceType.GetProperty("Template");

            nodeProperty.SetValue(metadataInstance, nodeTemplate);

            var connectionMapper = new ConnectionMapper(metadataInstance, singleNodeGraph);

            var inputsArray  = metadataInstance.Inputs(connectionMapper);
            var outputsArray = metadataInstance.Outputs(connectionMapper);

            if (inputsArray != null)
            {
                nodeInformation.Inputs = new Dictionary <string, SocketInformation>(inputsArray.Length);

                for (int i = 0; i < inputsArray.Length; i++)
                {
                    var map   = inputsArray[i];
                    var field = inputSocketFields[map.ConnectionId];
                    nodeInformation.Inputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Inputs = null;
            }

            if (outputsArray != null)
            {
                nodeInformation.Outputs = new Dictionary <string, SocketInformation>(outputsArray.Length);

                for (int i = 0; i < outputsArray.Length; i++)
                {
                    var map   = outputsArray[i];
                    var field = outputSocketFields[map.ConnectionId];
                    nodeInformation.Outputs.Add(field.Name, SocketInformation.Construct(field, map));
                }
            }
            else
            {
                nodeInformation.Outputs = null;
            }

            return(nodeInformation);
        }