public void ReadWriteAttributeTest() { DomAttribute attr = new DomAttribute(); CodePropertyReferenceExpression exp1 = new CodePropertyReferenceExpression(); exp1.TargetObject = new CodeTypeReferenceExpression("SomeType"); exp1.PropertyName = "SomeProperty"; CodeTypeOfExpression exp2 = new CodeTypeOfExpression("SomeTypeOf"); CodeBinaryOperatorExpression exp3 = new CodeBinaryOperatorExpression(); exp3.Left = new CodePrimitiveExpression("one"); exp3.Right = new CodePrimitiveExpression("two"); exp3.Operator = CodeBinaryOperatorType.Add; CodePrimitiveExpression exp4 = new CodePrimitiveExpression(37); attr.AddPositionalArgument(exp1); attr.AddPositionalArgument(exp2); attr.AddPositionalArgument(exp3); attr.AddPositionalArgument(exp4); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); DomPersistence.Write(writer, DefaultNameEncoder, attr); byte[] bytes = ms.ToArray(); DomAttribute result = DomPersistence.ReadAttribute(CreateReader(bytes), DefaultNameDecoder, null); Assert.AreEqual(4, result.PositionalArguments.Count); Assert.AreEqual(typeof(CodePropertyReferenceExpression), result.PositionalArguments [0].GetType()); CodePropertyReferenceExpression rexp1 = (CodePropertyReferenceExpression)result.PositionalArguments [0]; Assert.AreEqual(typeof(CodeTypeReferenceExpression), rexp1.TargetObject.GetType()); Assert.AreEqual("SomeType", ((CodeTypeReferenceExpression)rexp1.TargetObject).Type.BaseType); Assert.AreEqual("SomeProperty", rexp1.PropertyName); Assert.AreEqual(typeof(CodeTypeOfExpression), result.PositionalArguments [1].GetType()); Assert.AreEqual("SomeTypeOf", ((CodeTypeOfExpression)result.PositionalArguments [1]).Type.BaseType); Assert.AreEqual(typeof(CodeBinaryOperatorExpression), result.PositionalArguments [2].GetType()); CodeBinaryOperatorExpression rexp3 = (CodeBinaryOperatorExpression)result.PositionalArguments [2]; Assert.AreEqual(typeof(CodePrimitiveExpression), rexp3.Left.GetType()); Assert.AreEqual("one", ((CodePrimitiveExpression)rexp3.Left).Value); Assert.AreEqual(typeof(CodePrimitiveExpression), rexp3.Right.GetType()); Assert.AreEqual("two", ((CodePrimitiveExpression)rexp3.Right).Value); Assert.AreEqual(typeof(CodePrimitiveExpression), result.PositionalArguments [3].GetType()); Assert.AreEqual(37, ((CodePrimitiveExpression)result.PositionalArguments [3]).Value); }
public static DomAttribute ReadAttribute(BinaryReader reader, INameDecoder nameTable) { DomAttribute attr = new DomAttribute(); attr.Name = ReadString(reader, nameTable); attr.Region = ReadRegion(reader, nameTable); attr.AttributeTarget = (AttributeTarget)reader.ReadInt32(); attr.AttributeType = ReadReturnType(reader, nameTable); // Named argument count uint num = ReadUInt(reader, 500); string[] names = new string[num]; for (int n = 0; n < num; n++) { names [n] = ReadString(reader, nameTable); } CodeExpression[] exps = ReadExpressionArray(reader, nameTable); int i; for (i = 0; i < num; i++) { attr.AddNamedArgument(names[i], exps [i]); } for (; i < exps.Length; i++) { attr.AddPositionalArgument(exps [i]); } return(attr); }
static void AddAttributes (AbstractMember member, IEnumerable<ICSharpCode.NRefactory.Ast.AttributeSection> attributes) { CodeDomVisitor domVisitor = new CodeDomVisitor (); foreach (ICSharpCode.NRefactory.Ast.AttributeSection attributeSection in attributes) { foreach (ICSharpCode.NRefactory.Ast.Attribute attribute in attributeSection.Attributes) { DomAttribute domAttribute = new DomAttribute (); domAttribute.Name = attribute.Name; domAttribute.Region = ConvertRegion (attribute.StartLocation, attribute.EndLocation); domAttribute.AttributeType = new DomReturnType (attribute.Name); member.Add (domAttribute); foreach (ICSharpCode.NRefactory.Ast.Expression exp in attribute.PositionalArguments) domAttribute.AddPositionalArgument ((CodeExpression)exp.AcceptVisitor (domVisitor, null)); foreach (ICSharpCode.NRefactory.Ast.NamedArgumentExpression nexp in attribute.NamedArguments) domAttribute.AddNamedArgument (nexp.Name, (CodeExpression)nexp.Expression.AcceptVisitor (domVisitor, null)); } } }
IEnumerable<IAttribute> ConvertAttributes (Attributes optAttributes, IMemberContext mc) { List<IAttribute> atts = new List<IAttribute> (); if (optAttributes == null || optAttributes.Attrs == null) return atts; ResolveContext ctx = new ResolveContext (mc); foreach (var section in optAttributes.Sections) { foreach (var attr in section) { DomAttribute domAttribute = new DomAttribute (); domAttribute.Name = ConvertQuoted (attr.Name); domAttribute.Region = ConvertRegion (attr.Location, attr.Location); domAttribute.AttributeType = ConvertReturnType (attr.TypeNameExpression); if (attr.PosArguments != null) { for (int i = 0; i < attr.PosArguments.Count; i++) { CodeExpression domExp; var exp = attr.PosArguments [i].Expr; if (exp is TypeOf) { TypeOf tof = (TypeOf)exp; IReturnType rt = ConvertReturnType (tof.TypeExpression); domExp = new CodeTypeOfExpression (rt.FullName); } else if (exp is Binary) { // Currently unsupported in the old dom (will be in the new dom) continue; } else if (exp is Constant) { try { var res = exp.Resolve (ctx); var val = res as Constant; if (val == null) continue; domExp = new CodePrimitiveExpression (val.GetValue ()); } catch { continue; } } else { try { domExp = ResolveMemberAccessExpression (exp); } catch { continue; } } if (domExp != null) domAttribute.AddPositionalArgument (domExp); } } if (attr.NamedArguments != null) { for (int i = 0; i < attr.NamedArguments.Count; i++) { var val = attr.NamedArguments [i].Expr as Constant; if (val == null) continue; domAttribute.AddNamedArgument (((NamedArgument)attr.NamedArguments [i]).Name, new CodePrimitiveExpression (val.GetValue ())); } } atts.Add (domAttribute); } } return atts; }
public void AddAttributes (MonoDevelop.Projects.Dom.AbstractMember member, Attributes optAttributes) { if (optAttributes == null || optAttributes.Attrs == null) return; foreach (var attr in optAttributes.Attrs) { DomAttribute domAttribute = new DomAttribute (); domAttribute.Name = attr.Name; domAttribute.Region = ConvertRegion (attr.Location, attr.Location); domAttribute.AttributeType = new DomReturnType (attr.Name); if (attr.PosArguments != null) { for (int i = 0; i < attr.PosArguments.Count; i++) { var val = attr.PosArguments[i].Expr as Constant; if (val == null) { continue; } domAttribute.AddPositionalArgument (new CodePrimitiveExpression (val.GetValue ())); } } if (attr.NamedArguments != null) { for (int i = 0; i < attr.NamedArguments.Count; i++) { var val = attr.NamedArguments[i].Expr as Constant; if (val == null) continue; domAttribute.AddNamedArgument (((NamedArgument)attr.NamedArguments[i]).Name, new CodePrimitiveExpression (val.GetValue ())); } } member.Add (domAttribute); } }
public static DomAttribute ReadAttribute (BinaryReader reader, INameDecoder nameTable, IDomObjectTable objectTable) { DomAttribute attr = new DomAttribute (); attr.Name = ReadString (reader, nameTable); attr.Region = ReadRegion (reader, nameTable); attr.AttributeTarget = (AttributeTarget)reader.ReadInt32 (); attr.AttributeType = ReadReturnType (reader, nameTable, objectTable); // Named argument count uint num = ReadUInt (reader, 500); string[] names = new string[num]; for (int n=0; n<num; n++) names [n] = ReadString (reader, nameTable); CodeExpression[] exps = ReadExpressionArray (reader, nameTable); int i; for (i=0; i<num; i++) attr.AddNamedArgument (names [i], exps [i]); for (; i<exps.Length; i++) attr.AddPositionalArgument (exps [i]); return attr; }
public void ReadWriteAttributeTest () { DomAttribute attr = new DomAttribute (); CodePropertyReferenceExpression exp1 = new CodePropertyReferenceExpression (); exp1.TargetObject = new CodeTypeReferenceExpression ("SomeType"); exp1.PropertyName = "SomeProperty"; CodeTypeOfExpression exp2 = new CodeTypeOfExpression ("SomeTypeOf"); CodeBinaryOperatorExpression exp3 = new CodeBinaryOperatorExpression (); exp3.Left = new CodePrimitiveExpression ("one"); exp3.Right = new CodePrimitiveExpression ("two"); exp3.Operator = CodeBinaryOperatorType.Add; CodePrimitiveExpression exp4 = new CodePrimitiveExpression (37); attr.AddPositionalArgument (exp1); attr.AddPositionalArgument (exp2); attr.AddPositionalArgument (exp3); attr.AddPositionalArgument (exp4); MemoryStream ms = new MemoryStream (); BinaryWriter writer = new BinaryWriter (ms); DomPersistence.Write (writer, DefaultNameEncoder, attr); byte[] bytes = ms.ToArray (); DomAttribute result = DomPersistence.ReadAttribute (CreateReader (bytes), DefaultNameDecoder); Assert.AreEqual (4, result.PositionalArguments.Count); Assert.AreEqual (typeof(CodePropertyReferenceExpression), result.PositionalArguments [0].GetType ()); CodePropertyReferenceExpression rexp1 = (CodePropertyReferenceExpression) result.PositionalArguments [0]; Assert.AreEqual (typeof(CodeTypeReferenceExpression), rexp1.TargetObject.GetType ()); Assert.AreEqual ("SomeType", ((CodeTypeReferenceExpression)rexp1.TargetObject).Type.BaseType); Assert.AreEqual ("SomeProperty", rexp1.PropertyName); Assert.AreEqual (typeof(CodeTypeOfExpression), result.PositionalArguments [1].GetType ()); Assert.AreEqual ("SomeTypeOf", ((CodeTypeOfExpression)result.PositionalArguments [1]).Type.BaseType); Assert.AreEqual (typeof(CodeBinaryOperatorExpression), result.PositionalArguments [2].GetType ()); CodeBinaryOperatorExpression rexp3 = (CodeBinaryOperatorExpression) result.PositionalArguments [2]; Assert.AreEqual (typeof(CodePrimitiveExpression), rexp3.Left.GetType ()); Assert.AreEqual ("one", ((CodePrimitiveExpression)rexp3.Left).Value); Assert.AreEqual (typeof(CodePrimitiveExpression), rexp3.Right.GetType ()); Assert.AreEqual ("two", ((CodePrimitiveExpression)rexp3.Right).Value); Assert.AreEqual (typeof(CodePrimitiveExpression), result.PositionalArguments [3].GetType ()); Assert.AreEqual (37, ((CodePrimitiveExpression)result.PositionalArguments [3]).Value); }