C# 从Enumerable.where选择要列出的EnumerableIterator<;字符串>;
我需要为一个方法提供一组类型C# 从Enumerable.where选择要列出的EnumerableIterator<;字符串>;,c#,.net,linq,asp.net-core,asp.net-core-mvc,C#,.net,Linq,Asp.net Core,Asp.net Core Mvc,我需要为一个方法提供一组类型List,这些类型来自apiscription对象: public class AddTags : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methods) == true)
List
,这些类型来自apiscription
对象:
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();
}
}
}
test
变量返回类型为System.Linq.Enumerable.whereselectEnumerableTerator
的集合,但我需要类型为List
的结果
在控制器中,自定义标记的应用方式如下:
[CustomTag(Tag = "TEST")]
public ActionResult MyAction()
试试这个:
var tags = methods.GetCustomAttributes(typeof(CustomTag), true)
.OfType<CustomTag>()
.Select(attribute => attribute.Tag)
.ToList();
var tags=methods.GetCustomAttributes(typeof(CustomTag),true)
第()类
.Select(属性=>attribute.Tag)
.ToList();
顺便说一下,
CustomTag
应该真正命名为CustomTagAttribute
。它仍将作为[CustomTag]
工作,谢谢,但不会编译:'Attribute'不包含'OfType'的定义,而最佳扩展方法重载'Queryable.OfType(IQueryable)'需要'IQueryable'类型的接收器。
。我假设您正在使用System.Linq?是,我有使用System.Linq
和使用System.Reflection
那么这个错误就没有意义了。你是复制代码1:1还是键入的?您可以再次检查吗?表达式可以简化为var tags=methods.GetCustomAttributes(true).Select(attribute=>attribute.Tag).ToList()代码>,请参阅