示例#1
0
        public static Instance FromAstNode(InstanceValueDeclarationAst node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            var instance = new Instance
            {
                ClassName = node.TypeName.Name,
                Alias     = node?.Alias?.Name
            };

            foreach (var property in node.PropertyValues.PropertyValues)
            {
                var propertyValue = property.Value;
                if ((propertyValue as LiteralValueArrayAst) != null)
                {
                    var itemValues = ((LiteralValueArrayAst)propertyValue).Values
                                     .Select(Instance.GetLiteralValue)
                                     .ToArray();
                    instance.Properties.Add(property.Key, itemValues);
                }
                else if ((propertyValue as LiteralValueAst) != null)
                {
                    instance.Properties.Add(property.Key, Instance.GetLiteralValue((LiteralValueAst)propertyValue));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            return(instance);
        }
示例#2
0
        public static Instance FromAstNode(ComplexValueAst node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (!node.IsInstance)
            {
                throw new ArgumentException("Value must represent an instance.", "node");
            }
            var instance = new Instance
            {
                ClassName = node.TypeName,
                Alias     = node.Alias
            };

            foreach (var property in node.Properties)
            {
                var propertyValue = property.Value.Value;
                if ((propertyValue as LiteralValueArrayAst) != null)
                {
                    var itemValues = ((LiteralValueArrayAst)propertyValue).Values
                                     .Select(Instance.GetLiteralValue)
                                     .ToArray();
                    instance.Properties.Add(property.Key, itemValues);
                }
                else if ((propertyValue as LiteralValueAst) != null)
                {
                    instance.Properties.Add(property.Key, Instance.GetLiteralValue((LiteralValueAst)propertyValue));
                }
                else if ((propertyValue as ReferenceTypeValueAst) != null)
                {
                    instance.Properties.Add(property.Key, (((ReferenceTypeValueAst)propertyValue).Name));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            return(instance);
        }