C# Linq扫描程序集以查找具有特定属性和属性的方法

C# Linq扫描程序集以查找具有特定属性和属性的方法,c#,reflection,attributes,C#,Reflection,Attributes,我正在尝试编写一个linq语句,该语句将扫描整个执行程序集,以查找具有特定属性和特定属性值的方法 这是我的属性 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] public class ScenarioSetup : Attribute { public string ScenarioTitle { get; set; } public bool A

我正在尝试编写一个linq语句,该语句将扫描整个执行程序集,以查找具有特定属性和特定属性值的方法

这是我的属性

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class ScenarioSetup : Attribute
{
    public string ScenarioTitle { get; set; }

    public bool ActiveForBuild { get; set; }
}
这是一个使用它的方法的例子

[ScenarioSetup(ScenarioTitle = "R/S/T [Assessor Management] ASM.08 - Activate Assessor", ActiveForBuild = true)]
public void CreateInactiveAssessor()
{
    var assessor = ApiHelper.PostRequest(AssessorFactory.CreateAssessorApi("Woodwork"), Client, false);

    AddStateItem(assessor, StateItemTag.AssessorEntity);
}
因此,使用上面的示例,我想使用反射来查找具有
[ScenarioSetup]
属性的方法,然后检查该属性
ScenarioTitle
是否等于某个值(在本例中为“R/S/T[Assessor Management]ASM.08-Activate Assessor”)

这就是我目前所拥有的

var methodz = Assembly.GetExecutingAssembly().GetTypes()
            .SelectMany(t => t.GetMethods())
            .Where(m => m.GetCustomAttributes(typeof(ScenarioSetup), false).Length > 0);
然后,我在尝试执行
ScenarioTitle
检查时迷路了

有人能帮忙吗?

你可以试试:

var methodz = Assembly.GetExecutingAssembly().GetTypes()
                      .SelectMany(t => t.GetMethods())
                      .Where(m => m.GetCustomAttributes(typeof(ScenarioSetupAttribute), false)
                      .Cast<ScenarioSetupAttribute>().Where(a=>a.ScenarioTitle=="R/S/T [Assessor Management] ASM.08 - Activate Assessor").Length > 0);
var methodz=Assembly.getExecutionGassembly().GetTypes()
.SelectMany(t=>t.GetMethods())
.Where(m=>m.GetCustomAttributes(typeof(ScenarioSetupAttribute),false)
.Cast()。其中(a=>a.ScenarioTitle==“R/S/T[评估员管理]ASM.08-激活评估员”)。长度>0);
您可以尝试:

var methodz = Assembly.GetExecutingAssembly().GetTypes()
                      .SelectMany(t => t.GetMethods())
                      .Where(m => m.GetCustomAttributes(typeof(ScenarioSetupAttribute), false)
                      .Cast<ScenarioSetupAttribute>().Where(a=>a.ScenarioTitle=="R/S/T [Assessor Management] ASM.08 - Activate Assessor").Length > 0);
var methodz=Assembly.getExecutionGassembly().GetTypes()
.SelectMany(t=>t.GetMethods())
.Where(m=>m.GetCustomAttributes(typeof(ScenarioSetupAttribute),false)
.Cast()。其中(a=>a.ScenarioTitle==“R/S/T[评估员管理]ASM.08-激活评估员”)。长度>0);
另一种方法是:

`

var methods=Assembly.getExecutionGassembly()
.GetTypes()
.SelectMany(t=>t.GetMethods())
.Where(m=>m.GetCustomAttributes().Any())
其中(x=>x.GetCustomAttribute().ScenarioTitle==“R/S/T[评估员管理]ASM.08-激活评估员”)`
另一种方法是:

`

var methods=Assembly.getExecutionGassembly()
.GetTypes()
.SelectMany(t=>t.GetMethods())
.Where(m=>m.GetCustomAttributes().Any())
其中(x=>x.GetCustomAttribute().ScenarioTitle==“R/S/T[评估员管理]ASM.08-激活评估员”)`

您可以将
GetCustomAttributes
的结果视为
ScenarioSetup
的集合,然后您可以检查它们的标题是否匹配:
m.GetCustomAttributes(typeof(ScenarioSetup),false).OfType()。其中(ss=>ss.ScenarioTitle==“R/S/T[Assessor Management]ASM.08-Activate Assessor”)
编辑:顺便说一下,可以将LINQ查询拆分为两个或多个查询/步骤,甚至可以使用
foreach
循环。无需在单个查询中完成所有操作。您可以将
GetCustomAttributes
的结果视为
ScenarioSetup
的集合,然后可以检查它们的标题是否匹配:
m.GetCustomAttributes(typeof(ScenarioSetup),false)。OfType()。其中(ss=>ss.ScenarioTitle==“R/S/T[评估员管理]ASM.08-激活Assessor”)
EDIT:顺便说一下,可以将LINQ查询拆分为两个或多个查询/步骤,甚至可以使用
foreach
循环。不需要在一个查询中包含所有内容。