[英]From Enumerable.WhereSelectEnumerableIterator to List<string>
I need to feed a method with a collection of type List<string>
coming from ApiDescription
objects:我需要使用来自ApiDescription
对象的List<string>
类型集合提供一个方法:
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>
. test
变量返回System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Reflection.CustomAttributeNamedArgument, string>
类型的集合,但我需要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
.顺便说一下, CustomTag
应该真正命名为CustomTagAttribute
。 It will still work as [CustomTag]
.它仍然可以用作[CustomTag]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.