コード例 #1
0
ファイル: Instance.cs プロジェクト: sergey-raevskiy/MofParser
 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 ReferenceTypeValue) != null)
         {
             instance.Properties.Add(property.Key, (((ReferenceTypeValue)propertyValue).Name));
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
     return instance;
 }