private static Tuple <List <Operation>, List <Field> > FakeSpec() { var operations = new List <Operation>(); var fields = new List <Field>(); IReadOnlyDictionary <MacroOperationType, Tuple <ProtocolVersion, Type> > types = MacroOpManager.FindAll(); foreach (var t in types) { MacroOperationType id = t.Key; Type type = t.Value.Item2; IEnumerable <PropertyInfo> props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(prop => prop.GetCustomAttribute <NoSerializeAttribute>() == null); var opFields = new List <OperationField>(); foreach (PropertyInfo prop in props) { var fieldAttr = prop.GetCustomAttribute <MacroFieldAttribute>(); if (fieldAttr == null) { continue; } Tuple <string, bool> mappedType = TypeMappings.MapTypeFull(prop.PropertyType); fields.Add(new Field(fieldAttr.Id, fieldAttr.Name, mappedType.Item1, mappedType.Item2, prop.PropertyType.GetCustomAttributes <FlagsAttribute>().Any() || prop.PropertyType.GetCustomAttributes <XmlAsStringAttribute>().Any())); opFields.Add(new OperationField(fieldAttr.Id, fieldAttr.Name, prop.Name, prop.PropertyType.ToString())); } operations.Add(new Operation(id.ToString(), type.FullName, opFields, type.GetCustomAttributes <NoMacroFieldsAttribute>().Any())); } fields = fields.Distinct().OrderBy(f => f.Id).ToList(); operations = operations.OrderBy(o => o.Id).ToList(); var res = new List <Field>(); foreach (IGrouping <string, Field> grp in fields.GroupBy(f => f.Id)) { FieldEntry[] newTypes = grp.SelectMany(g => g.Entries).ToArray(); Field f = grp.First(); res.Add(new Field(f.Id, newTypes, f.EnumAsString, f.IsEnum)); } return(Tuple.Create(operations, res)); }
private static ReturnStatementSyntax GenerateXmlToMacroOp(Operation op) { var props = SyntaxFactory.SeparatedList <ExpressionSyntax>(); foreach (OperationField f in op.Fields) { var expr = SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(f.PropName), ConvertVariable(SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("mac"), SyntaxFactory.IdentifierName(f.FieldName)), TypeMappings.MapType(f.Type), f.Type)); props = props.Add(expr); } var inst = SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(op.Classname)) .WithNewKeyword(SyntaxFactory.Token(SyntaxKind.NewKeyword)).WithInitializer(SyntaxFactory.InitializerExpression(SyntaxKind.ObjectInitializerExpression, props)); return(SyntaxFactory.ReturnStatement(inst)); }