简体   繁体   中英

From Enumerable.WhereSelectEnumerableIterator to List<string>

I need to feed a method with a collection of type List<string> coming from ApiDescription objects:

public class AddTags : IOperationFilter
{
   public void Apply(Operation operation, OperationFilterContext context)
   {
      if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methods) == true)
      {
         var test = methods.CustomAttributes
            .Where(x => x.AttributeType == typeof(CustomTag))
            .Select(x => x.NamedArguments.Where(n => n.MemberName.Equals("Tag"))
                          .Select(t => (string)t.TypedValue.Value))
            .ToList();
      }
   }
}

The test variable returns a collection of type System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Reflection.CustomAttributeNamedArgument, string> but I need a result of type List<string> .

The custom tag is applied like this in controllers:

[CustomTag(Tag = "TEST")]
public ActionResult MyAction()

Try this:

var tags = methods.GetCustomAttributes(typeof(CustomTag), true)
                  .OfType<CustomTag>()
                  .Select(attribute => attribute.Tag)
                  .ToList();

By the way, CustomTag should really be named CustomTagAttribute . It will still work as [CustomTag] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM