Beispiel #1
0
 public DefaultAttribute(string name, AttributeTarget attributeTarget)
 {
     _name                = name;
     _attributeTarget     = attributeTarget;
     _positionalArguments = new List <AttributeArgument>();
     _namedArguments      = new SortedList <string, AttributeArgument>();
 }
Beispiel #2
0
 protected AttributeInfo(AttributeTarget target, ClassFile classFile, ushort nameIndex, uint attributeSize, byte[] attributePayload = null) : base(classFile)
 {
     Target           = target;
     NameIndex        = nameIndex;
     AttributeSize    = attributeSize;
     AttributePayload = attributePayload;
 }
Beispiel #3
0
 public DefaultAttribute(string name, AttributeTarget attributeTarget, List <AttributeArgument> positionalArguments, SortedList <string, AttributeArgument> namedArguments)
 {
     this.name                = name;
     this.attributeTarget     = attributeTarget;
     this.positionalArguments = positionalArguments;
     this.namedArguments      = namedArguments;
 }
Beispiel #4
0
 public DefaultAttribute(string name, AttributeTarget attributeTarget)
 {
     this.name                = name;
     this.attributeTarget     = attributeTarget;
     this.positionalArguments = new List <AttributeArgument>();
     this.namedArguments      = new SortedList <string, AttributeArgument>();
 }
Beispiel #5
0
        public static string GetAttributeTargetName(AttributeTarget attributeTarget)
        {
            switch (attributeTarget)
            {
            case AttributeTarget.None:
                return(null);

            case AttributeTarget.Assembly:
                return("assembly");

            case AttributeTarget.Module:
                return("module");

            case AttributeTarget.Type:
                return("type");

            case AttributeTarget.Param:
                return("param");

            case AttributeTarget.Field:
                return("field");

            case AttributeTarget.Return:
                return("return");

            case AttributeTarget.Method:
                return("method");

            default:
                throw new NotSupportedException("Invalid value for AttributeTarget");
            }
        }
		public DefaultAttribute(string name, AttributeTarget attributeTarget, List<AttributeArgument> positionalArguments, SortedList<string, AttributeArgument> namedArguments)
		{
			this.name = name;
			this.attributeTarget = attributeTarget;
			this.positionalArguments = positionalArguments;
			this.namedArguments = namedArguments;
		}
		public DefaultAttribute(string name, AttributeTarget attributeTarget)
		{
			this.name = name;
			this.attributeTarget = attributeTarget;
			this.positionalArguments = new List<AttributeArgument>();
			this.namedArguments = new SortedList<string, AttributeArgument>();
		}
 /// <summary>
 /// Create an <see cref="Attribute"/>.
 /// </summary>
 public Attribute(AttributeTarget target, params TypeRefBase[] typeRefBases)
 {
     _target = target;
     foreach (TypeRefBase typeRefBase in typeRefBases)
     {
         CreateAttributeExpressions().Add(ConstructorRef.Find(typeRefBase));
     }
 }
 /// <summary>
 /// Create an <see cref="Attribute"/>.
 /// </summary>
 public Attribute(AttributeTarget target, params Expression[] attributeExpressions)
 {
     _target = target;
     CreateAttributeExpressions().AddRange(attributeExpressions);
     foreach (Expression attributeCall in attributeExpressions)
     {
         attributeCall.FormatAsArgument();
     }
 }
		public DefaultAttribute(IReturnType attributeType, AttributeTarget attributeTarget, IList<object> positionalArguments, IDictionary<string, object> namedArguments)
		{
			if (attributeType == null)
				throw new ArgumentNullException("attributeType");
			this.AttributeType = attributeType;
			this.AttributeTarget = attributeTarget;
			this.positionalArguments = positionalArguments ?? new List<object>();
			this.namedArguments = namedArguments ?? new SortedList<string, object>();
		}
        /// <summary>
        /// Format Target as a string.
        /// </summary>
        public static string AsString(AttributeTarget target)
        {
            string result = "";
            string name;

            if (_targetToName.TryGetValue(target, out name))
            {
                result = name;
            }
            return(result);
        }
Beispiel #12
0
        public TargetedAttribute(AttributeTarget target, Type type, IEnumerable <string> args)
        {
            if (!typeof(Attribute).IsAssignableFrom(type))
            {
                throw new ArgumentException("Attribute type required", nameof(type));
            }

            Type   = type;
            Target = target;
            Value  = string.Join(", ", args);
        }
Beispiel #13
0
 public DefaultAttribute(IReturnType attributeType, AttributeTarget attributeTarget, IList <object> positionalArguments, IDictionary <string, object> namedArguments)
 {
     if (attributeType == null)
     {
         throw new ArgumentNullException("attributeType");
     }
     this.AttributeType       = attributeType;
     this.AttributeTarget     = attributeTarget;
     this.positionalArguments = positionalArguments ?? new List <object>();
     this.namedArguments      = namedArguments ?? new SortedList <string, object>();
 }
        /// <summary>
        /// Parse token into Target enum.
        /// </summary>
        public static AttributeTarget Parse(Parser parser)
        {
            AttributeTarget target = 0;
            AttributeTarget temp;

            if (_nameToTarget.TryGetValue(parser.TokenText, out temp))
            {
                target = temp;
            }
            parser.NextToken();  // Move past target
            parser.NextToken();  // Move past ':'
            return(target);
        }
        protected Attribute(Parser parser, CodeObject parent)
            : base(parser, parent)
        {
            parser.NextToken();  // Move past '['

            if (parser.PeekNextTokenText() == ParseTokenTarget)
            {
                _target = AttributeTargetHelpers.Parse(parser);
            }

            // Parse attribute expressions (will parse to a Call if parens are used, otherwise a ConstructorRef)
            _attributeExpressions = Expression.ParseList(parser, this, ParseTokenEnd);

            ParseExpectedToken(parser, ParseTokenEnd);  // Move past ']'
        }
        public void AttributeWithTarget(string code, AttributeTarget target)
        {
            Test(
                @"[" + code + @": Attribute]
",
                new AttributeListSyntax
            {
                Target     = target,
                Attributes =
                {
                    new AttributeSyntax
                    {
                        Name = (NameSyntax)Syntax.ParseName("Attribute")
                    }
                }
            }
                );
        }
Beispiel #17
0
        public CodeAttributeInfo(AttributeTarget target, ClassFile classFile, ushort nameIndex, BigEndianBinaryReader reader) : base(target, classFile, nameIndex, reader.ReadUInt32())
        {
            MaxStack  = reader.ReadUInt16();
            MaxLocals = reader.ReadUInt16();

            CodeLength = reader.ReadUInt32();
            Codes      = reader.ReadBytes((int)CodeLength);

            ExceptionTableCount = reader.ReadUInt16();
            ExceptionTable      = new ExceptionInfo[ExceptionTableCount];

            for (var i = 0; i < ExceptionTableCount; i++)
            {
                ExceptionTable[i] = new ExceptionInfo(classFile, this, reader);
            }

            AttributesCount = reader.ReadUInt16();
            Attributes      = new AttributeInfo[AttributesCount];
            for (var i = 0; i < AttributesCount; i++)
            {
                Attributes[i] = GetAttributeInfo(target | AttributeTarget.CodeAttribute, classFile, reader);
            }
        }
		public DefaultAttribute(IReturnType attributeType, AttributeTarget attributeTarget)
			: this(attributeType, attributeTarget, null, null)
		{
		}
Beispiel #19
0
        List <IAttribute> VisitAttributes(IList <AST.AttributeSection> attributes, ClassFinder context)
        {
            // TODO Expressions???
            List <IAttribute> result = new List <IAttribute>();

            foreach (AST.AttributeSection section in attributes)
            {
                AttributeTarget target = AttributeTarget.None;
                if (section.AttributeTarget != null && section.AttributeTarget != "")
                {
                    switch (section.AttributeTarget.ToUpperInvariant())
                    {
                    case "ASSEMBLY":
                        target = AttributeTarget.Assembly;
                        break;

                    case "FIELD":
                        target = AttributeTarget.Field;
                        break;

                    case "EVENT":
                        target = AttributeTarget.Event;
                        break;

                    case "METHOD":
                        target = AttributeTarget.Method;
                        break;

                    case "MODULE":
                        target = AttributeTarget.Module;
                        break;

                    case "PARAM":
                        target = AttributeTarget.Param;
                        break;

                    case "PROPERTY":
                        target = AttributeTarget.Property;
                        break;

                    case "RETURN":
                        target = AttributeTarget.Return;
                        break;

                    case "TYPE":
                        target = AttributeTarget.Type;
                        break;

                    default:
                        target = AttributeTarget.None;
                        break;
                    }
                }

                foreach (AST.Attribute attribute in section.Attributes)
                {
                    List <object> positionalArguments = new List <object>();
                    foreach (AST.Expression positionalArgument in attribute.PositionalArguments)
                    {
                        positionalArguments.Add(ConvertAttributeArgument(positionalArgument));
                    }
                    Dictionary <string, object> namedArguments = new Dictionary <string, object>();
                    foreach (AST.NamedArgumentExpression namedArgumentExpression in attribute.NamedArguments)
                    {
                        namedArguments.Add(namedArgumentExpression.Name, ConvertAttributeArgument(namedArgumentExpression.Expression));
                    }
                    result.Add(new DefaultAttribute(new AttributeReturnType(context, attribute.Name),
                                                    target, positionalArguments, namedArguments)
                    {
                        CompilationUnit = cu,
                        Region          = GetRegion(attribute.StartLocation, attribute.EndLocation)
                    });
                }
            }
            return(result);
        }
Beispiel #20
0
 /// <summary>
 /// Creates a new SiteMapDescriptionAttribute instance.
 /// </summary>
 /// <param name="propertyName">Property in ViewData used as the SiteMap.CurrentNode.Description</param>
 public SiteMapDescriptionAttribute(string propertyName)
 {
     this.PropertyName = propertyName;
     this.Target       = AttributeTarget.CurrentNode;
 }
Beispiel #21
0
 public DefaultAttribute(IReturnType attributeType, AttributeTarget attributeTarget)
     : this(attributeType, attributeTarget, null, null)
 {
 }
Beispiel #22
0
 /// <summary>
 /// Creates a new SiteMapTitleAttribute instance.
 /// </summary>
 /// <param name="propertyName">Property in ViewData used as the SiteMap.CurrentNode.Title</param>
 public SiteMapTitleAttribute(string propertyName)
 {
     this.PropertyName = propertyName;
     this.Target       = AttributeTarget.CurrentNode;
 }
 public void AttributeWithTarget(string code, AttributeTarget target)
 {
     Test(
     @"[" + code + @": Attribute]
     ",
         new AttributeListSyntax
         {
             Target = target,
             Attributes =
             {
                 new AttributeSyntax
                 {
                     Name = (NameSyntax)Syntax.ParseName("Attribute")
                 }
             }
         }
     );
 }
Beispiel #24
0
        public static AttributeInfo GetAttributeInfo(AttributeTarget target, ClassFile classFile, BigEndianBinaryReader reader)
        {
            ushort indexName = reader.ReadUInt16();
            string name      = ((ConstantUtf8Info)classFile.Constants[indexName]).Value;

            switch (name)
            {
            case "ConstantValue":
                break;

            case "Code":
                return(new CodeAttributeInfo(target, classFile, indexName, reader));

            case "StackMapTable":
                break;

            case "Exceptions":
                break;

            case "InnerClasses":
                break;

            case "EnclosingMethod":
                break;

            case "Synthetic":
                break;

            case "Signature":
                break;

            case "SourceFile":
                break;

            case "SourceDebugExtension":
                break;

            case "LineNumberTable":
                break;

            case "LocalVariableTable":
                break;

            case "LocalVariableTypeTable":
                break;

            case "Deprecated":
                break;

            case "RuntimeVisibleAnnotations":
                break;

            case "RuntimeInvisibleAnnotations":
                break;

            case "RuntimeVisibleParameterAnnotations":
                break;

            case "RuntimeInvisibleParameterAnnotations":
                break;

            case "AnnotationDefault":
                break;

            case "BootstrapMethods":
                break;
            }

            uint attributeCount = reader.ReadUInt32();
            var  bytes          = reader.ReadBytes((int)attributeCount);

            return(new AttributeInfo(target, classFile, indexName, attributeCount, bytes));
        }
Beispiel #25
0
		internal static void ConvertAttributes(AttributedNode attributedNode, FieldDefinition fieldDefinition, AttributeTarget target = AttributeTarget.None)
		{
			ConvertCustomAttributes(attributedNode, fieldDefinition);
			
			#region FieldOffsetAttribute
			if (fieldDefinition.HasLayoutInfo) {
				Ast.Attribute fieldOffset = CreateNonCustomAttribute(typeof(FieldOffsetAttribute), fieldDefinition.Module);
				fieldOffset.Arguments.Add(new PrimitiveExpression(fieldDefinition.Offset));
				attributedNode.Attributes.Add(new AttributeSection(fieldOffset) { AttributeTarget = target });
			}
			#endregion
			
			#region NonSerializedAttribute
			if (fieldDefinition.IsNotSerialized) {
				Ast.Attribute nonSerialized = CreateNonCustomAttribute(typeof(NonSerializedAttribute), fieldDefinition.Module);
				attributedNode.Attributes.Add(new AttributeSection(nonSerialized) { AttributeTarget = target });
			}
			#endregion
			
			if (fieldDefinition.HasMarshalInfo) {
				attributedNode.Attributes.Add(new AttributeSection(ConvertMarshalInfo(fieldDefinition, fieldDefinition.Module))  { AttributeTarget = target });
			}
		}
 /// <summary>
 /// Create an <see cref="Attribute"/>.
 /// </summary>
 public Attribute(AttributeTarget target, SymbolicRef constructorRef, params Expression[] arguments)
     : this(target, (arguments.Length > 0 ? (Expression) new Call(constructorRef, arguments) : constructorRef))
 {
 }
Beispiel #27
0
 public TargetedAttribute(AttributeTarget target, Type type, params string[] args)
     : this(target, type, (IEnumerable <string>)args)
 {
 }
 public static void AsTextAttributes(CodeWriter writer, ParameterInfo parameterInfo, AttributeTarget attributeTarget)
 {
     // Use the static method to get the attributes so that this works with types from reflection-only assemblies
     AsTextAttributes(writer, CustomAttributeData.GetCustomAttributes(parameterInfo), attributeTarget);
 }
        protected static void AsTextAttributes(CodeWriter writer, IList <CustomAttributeData> attributes, AttributeTarget attributeTarget)
        {
            int count = 0;

            foreach (CustomAttributeData attribute in attributes)
            {
                Type declaringType = attribute.Constructor.DeclaringType;
                if (declaringType != null)
                {
                    string name = declaringType.Name;
                    if (count > 0)
                    {
                        writer.Write(" ");
                    }
                    writer.Write(ParseTokenStart);
                    if (attributeTarget != AttributeTarget.None)
                    {
                        writer.Write(AttributeTargetHelpers.AsString(attributeTarget) + ": ");
                    }
                    if (name.EndsWith(NameSuffix))
                    {
                        name = name.Substring(0, name.Length - NameSuffix.Length);
                    }
                    writer.Write(name);

                    if (attribute.ConstructorArguments.Count > 0)
                    {
                        writer.Write(ParameterDecl.ParseTokenStart);
                        foreach (CustomAttributeTypedArgument argument in attribute.ConstructorArguments)
                        {
                            AsTextValue(writer, argument.Value);
                        }
                        writer.Write(ParameterDecl.ParseTokenEnd);
                    }
                    else if (attribute.NamedArguments != null && attribute.NamedArguments.Count > 0)
                    {
                        writer.Write(ParameterDecl.ParseTokenStart);
                        foreach (CustomAttributeNamedArgument argument in attribute.NamedArguments)
                        {
                            writer.Write(argument.MemberInfo.Name + " = ");
                            AsTextValue(writer, argument.TypedValue.Value);
                        }
                        writer.Write(ParameterDecl.ParseTokenEnd);
                    }

                    writer.Write(ParseTokenEnd);
                    ++count;
                }
            }
            if (count > 0)
            {
                writer.WriteLine();
            }
        }
        List <IAttribute> VisitAttributes(List <AST.AttributeSection> attributes)
        {
            // TODO Expressions???
            List <IAttribute> result = new List <IAttribute>();

            foreach (AST.AttributeSection section in attributes)
            {
                AttributeTarget target = AttributeTarget.None;
                if (section.AttributeTarget != null && section.AttributeTarget != "")
                {
                    switch (section.AttributeTarget.ToUpperInvariant())
                    {
                    case "ASSEMBLY":
                        target = AttributeTarget.Assembly;
                        break;

                    case "FIELD":
                        target = AttributeTarget.Field;
                        break;

                    case "EVENT":
                        target = AttributeTarget.Event;
                        break;

                    case "METHOD":
                        target = AttributeTarget.Method;
                        break;

                    case "MODULE":
                        target = AttributeTarget.Module;
                        break;

                    case "PARAM":
                        target = AttributeTarget.Param;
                        break;

                    case "PROPERTY":
                        target = AttributeTarget.Property;
                        break;

                    case "RETURN":
                        target = AttributeTarget.Return;
                        break;

                    case "TYPE":
                        target = AttributeTarget.Type;
                        break;

                    default:
                        target = AttributeTarget.None;
                        break;
                    }
                }

                foreach (AST.Attribute attribute in section.Attributes)
                {
                    result.Add(new DefaultAttribute(attribute.Name, target));
                }
            }
            return(result);
        }
 public AttributeSection(AttributeTarget attributeTarget,
                         AttributeCollection attributes)
 {
     this.attributeTarget = attributeTarget;
     this.attributes      = attributes;
 }
Beispiel #32
0
		static void ConvertCustomAttributes(AstNode attributedNode, ICustomAttributeProvider customAttributeProvider, AttributeTarget target = AttributeTarget.None)
		{
			if (customAttributeProvider.HasCustomAttributes) {
				var attributes = new List<ICSharpCode.NRefactory.CSharp.Attribute>();
				foreach (var customAttribute in customAttributeProvider.CustomAttributes) {
					var attribute = new ICSharpCode.NRefactory.CSharp.Attribute();
					attribute.AddAnnotation(customAttribute);
					attribute.Type = ConvertType(customAttribute.AttributeType);
					attributes.Add(attribute);
					
					SimpleType st = attribute.Type as SimpleType;
					if (st != null && st.Identifier.EndsWith("Attribute", StringComparison.Ordinal)) {
						st.Identifier = st.Identifier.Substring(0, st.Identifier.Length - "Attribute".Length);
					}

					if(customAttribute.HasConstructorArguments) {
						foreach (var parameter in customAttribute.ConstructorArguments) {
							Expression parameterValue = ConvertArgumentValue(parameter);
							attribute.Arguments.Add(parameterValue);
						}
					}
					if (customAttribute.HasProperties) {
						TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve();
						foreach (var propertyNamedArg in customAttribute.Properties) {
							var propertyReference = resolvedAttributeType != null ? resolvedAttributeType.Properties.FirstOrDefault(pr => pr.Name == propertyNamedArg.Name) : null;
							var propertyName = new IdentifierExpression(propertyNamedArg.Name).WithAnnotation(propertyReference);
							var argumentValue = ConvertArgumentValue(propertyNamedArg.Argument);
							attribute.Arguments.Add(new AssignmentExpression(propertyName, argumentValue));
						}
					}

					if (customAttribute.HasFields) {
						TypeDefinition resolvedAttributeType = customAttribute.AttributeType.Resolve();
						foreach (var fieldNamedArg in customAttribute.Fields) {
							var fieldReference = resolvedAttributeType != null ? resolvedAttributeType.Fields.FirstOrDefault(f => f.Name == fieldNamedArg.Name) : null;
							var fieldName = new IdentifierExpression(fieldNamedArg.Name).WithAnnotation(fieldReference);
							var argumentValue = ConvertArgumentValue(fieldNamedArg.Argument);
							attribute.Arguments.Add(new AssignmentExpression(fieldName, argumentValue));
						}
					}
				}

				if (target == AttributeTarget.Module || target == AttributeTarget.Assembly) {
					// use separate section for each attribute
					foreach (var attribute in attributes) {
						var section = new AttributeSection();
						section.AttributeTarget = target;
						section.Attributes.Add(attribute);
						attributedNode.AddChild(section, AttributedNode.AttributeRole);
					}
				} else {
					// use single section for all attributes
					var section = new AttributeSection();
					section.AttributeTarget = target;
					section.Attributes.AddRange(attributes);
					attributedNode.AddChild(section, AttributedNode.AttributeRole);
				}
			}
		}
 public AttributeSection(AttributeTarget attributeTarget,
     AttributeCollection attributes)
 {
     this.attributeTarget = attributeTarget;
     this.attributes = attributes;
 }
Beispiel #34
0
        public static AttributeListSyntax AttributeList(AttributeTarget target = default(AttributeTarget), IEnumerable<AttributeSyntax> attributes = null)
        {
            var result = new AttributeListSyntax();

            result.Target = target;
            if (attributes != null)
                result.Attributes.AddRange(attributes);

            return result;
        }