private void GenerateEnumType(Type t) { EnumDeclaration e = this.ns.Enums[this.conformer.ToCapitalized(t.Name)]; e.CustomAttributes.Add(typeof(SerializableAttribute)); if (e == null) { throw new Exception(); } // generate fields and properties foreach (FieldInfo f in t.GetFields()) { if (f.Name == "Value__") { continue; } FieldDeclaration field = e.AddField(f.Name); // add XmlEnum attribute AttributeDeclaration xmlEnum = field.CustomAttributes.Add(typeof(XmlEnumAttribute)); AttributeArgument arg = xmlEnum.Arguments.Add( "Name", Expr.Prim(f.Name) ); } }
public void AttributeArgument_ParseShouldFail() { // Arrange AttributeArgument argument = new AttributeArgument(); IStringReader reader = new StringReader("foo::bar"); // Act ReadResults readResults = argument.Parse(reader, out _); // Assert Assert.IsFalse(readResults.Successful); }
private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, Enum outcome, string message, int primaryILOffset, int methodILOffset, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType) { IFeedbackManager feedbackManager = GetFeedbackConnection(); bool success = false; if (feedbackManager != null) { PropertyInfo outcomeProp = attributeType.GetProperty("Outcome"); PropertyInfo messageProp = attributeType.GetProperty("Message"); PropertyInfo primaryILProp = attributeType.GetProperty("PrimaryILOffset"); PropertyInfo methodILProp = attributeType.GetProperty("MethodILOffset"); ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[0]); MethodDefinitionName target = Translate(method, output, mdDecoder); MethodName attributeCtorName = MetadataFromReflection.GetMethod(attrCtor).SerializableName; #if DEBUG_PEX_BY_XML SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true); target.WriteXml(writer, "method"); writer.Close(); SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true); attributeCtorName2.WriteXml(writer2, "method"); writer2.Close(); #endif string group = Guid.NewGuid().ToString(); var outcomeArg = AttributeArgument.Named(MetadataFromReflection.GetProperty(outcomeProp), MetadataExpression.EnumValue(outcome)); #if DEBUG_PEX_BY_XML SafeSimpleXmlWriter writer3 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "3.xml"), true); outcomeArg.WriteXml(writer3, "method"); writer3.Close(); #endif CodeUpdate update = CodeUpdate.AddAttribute("Regression", target, attributeCtorName, outcomeArg, AttributeArgument.Named(MetadataFromReflection.GetProperty(messageProp), MetadataExpression.String(message)), AttributeArgument.Named(MetadataFromReflection.GetProperty(primaryILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), primaryILOffset)), AttributeArgument.Named(MetadataFromReflection.GetProperty(methodILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), methodILOffset)) ); CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message); try { feedbackManager.AddFix(fix); success = true; } catch { } } return(success); }
/// <summary> /// Adds an element with the specified key and value to this StringAttributeArgumentDictionary. /// </summary> /// <param name="key"> /// The string key of the element to add. /// </param> /// <param name="value"> /// The AttributeArgument value of the element to add. /// </param> public virtual AttributeArgument Add(string name, Expression expression) { if (name == null) { throw new ArgumentNullException("name"); } if (expression == null) { throw new ArgumentNullException("expression"); } AttributeArgument arg = new AttributeArgument(name, expression); this.Dictionary.Add(arg.Name, arg); return(arg); }
private void AddField(ClassDeclaration c, FieldInfo f) { if (c == null) { throw new ArgumentNullException("c"); } if (f == null) { throw new ArgumentNullException("f"); } FieldDeclaration fd = c.AddField(MapType(f.FieldType), f.Name); PropertyDeclaration p = c.AddProperty(fd, f.Name, true, true, false); // adding attributes if (TypeHelper.HasCustomAttribute(f, typeof(XmlAttributeAttribute))) { XmlAttributeAttribute att = (XmlAttributeAttribute)TypeHelper.GetFirstCustomAttribute(f, typeof(XmlAttributeAttribute)); AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlAttributeAttribute)); string attrName = att.AttributeName; if (att.AttributeName.Length == 0) { attrName = f.Name; } AttributeArgument arg = attr.Arguments.Add( "AttributeName", Expr.Prim(attrName) ); } else { if (TypeHelper.HasCustomAttribute(f, typeof(XmlElementAttribute))) { AttachXmlElementAttributes(p, f); } else { AttributeDeclaration attr = p.CustomAttributes.Add(typeof(XmlElementAttribute)); attr.Arguments.Add("ElementName", Expr.Prim(f.Name)); } } }
/// <summary> /// Adds an element with the specified key and value to this StringAttributeArgumentDictionary. /// </summary> /// <param name="key"> /// The string key of the element to add. /// </param> /// <param name="value"> /// The AttributeArgument value of the element to add. /// </param> public virtual AttributeArgument Add(string name, Expression expression) { if(name==null) throw new ArgumentNullException("name"); if (expression==null) throw new ArgumentNullException("expression"); AttributeArgument arg=new AttributeArgument(name,expression); this.Dictionary.Add(arg.Name,arg); return arg; }