Beispiel #1
0
        private void CheckCtor(SyntaxTree tree, Type objType, ObjectNode objNode)
        {
            ConstructorInfo   ctor;
            List <IValueNode> args = null;

            if (objNode.ConstructorArguments != null)
            {
                args = objNode.ConstructorArguments.ToList();
            }

            if (objNode.ConstructorArguments == null || args.Count == 0)
            {
                ctor = objType.GetConstructor(Type.EmptyTypes);
                if (ctor != null)
                {
                    return;
                }
            }
            else
            {
                var types = new Type[args.Count];

                var strType    = typeof(string);
                var doubleType = typeof(double);

                for (int i = 0; i < types.Length; i++)
                {
                    var t = args[0];

                    var refType = t as ReferenceNode;
                    if (refType != null)
                    {
                        types[i] = CheckType(refType.Type);
                    }
                    else if (t is StringNode)
                    {
                        types[i] = strType;
                    }
                    else if (t is NumberNode)
                    {
                        types[i] = doubleType;
                    }
                    else if (t is EnumNode)
                    {
                        types[i] = CheckType(((EnumNode)t).Type);
                    }
                    else
                    {
                        // todo: error message
                        throw new EdgeParserException();
                    }
                }

                ctor = objType.GetConstructor(types);
                if (ctor != null)
                {
                    return;
                }

                // ctor type inference
                // todo: refactor!!!
                var avaliableCtors = objType.GetConstructors()
                                     .Select(t => t.GetParameters())
                                     .Where(t => t.Length == types.Length)
                                     .ToArray();
                var uriType = typeof(Uri);

                for (int i = 0; i < avaliableCtors.Length && ctor == null; i++)
                {
                    var currentCtor = avaliableCtors[i];

                    for (int j = 0; j < currentCtor.Length && ctor == null; j++)
                    {
                        if (currentCtor[j].ParameterType == uriType && types[j] == strType)
                        {
                            types[j] = uriType;

                            // id
                            var    urlStrType = char.ToLowerInvariant(uriType.Name[0]) + uriType.Name.Substring(1);
                            string id         = null;
                            for (int k = 1; k < int.MaxValue; k++)
                            {
                                id = urlStrType + k;
                                if (!tree.Objects.Any(obj => obj.Id == id))
                                {
                                    break;
                                }
                            }
                            if (id == null)
                            {
                                // todo: message
                                throw new EdgeAnalyzerException();
                            }

                            tree.AddObject(new ObjectNode(uriType.Name, id, new[] { args[j] }));
                            args[j] = new ReferenceNode(id, uriType.Name);

                            ctor = objType.GetConstructor(types);
                        }
                    }
                }

                // todo: fix
                if (ctor != null)
                {
                    objNode.ConstructorArguments = args;

                    return;
                }
            }

            // todo: error message
            throw new EdgeAnalyzerException();
        }