使用c#扫描所有程序集时,是否有方法标识自定义程序集?
我有一个多项目的应用程序。当然,每个项目都会创建自己的dll。然后我们可以使用使用c#扫描所有程序集时,是否有方法标识自定义程序集?,c#,.net,vb.net,reflection,.net-assembly,C#,.net,Vb.net,Reflection,.net Assembly,我有一个多项目的应用程序。当然,每个项目都会创建自己的dll。然后我们可以使用AppDomain.CurrentDomain.getAssemblys().ToList()让所有程序集使用反射,原因有很多 AppDomain.CurrentDomain.getAssemblys()将扫描存在的每个程序集,包括像Microsoft这样的标准组件或我们使用其他依赖项管理工具的Nuget提取的包。如果我只想扫描我的项目,而不想扫描其他项目,该怎么办 有没有办法给我的项目一个共享类型,然后寻找那个共享类
AppDomain.CurrentDomain.getAssemblys().ToList()
让所有程序集使用反射,原因有很多
AppDomain.CurrentDomain.getAssemblys()
将扫描存在的每个程序集,包括像Microsoft这样的标准组件或我们使用其他依赖项管理工具的Nuget提取的包。如果我只想扫描我的项目,而不想扫描其他项目,该怎么办
有没有办法给我的项目一个共享类型,然后寻找那个共享类型?不幸的是,我的项目没有一个通用名称架构来扫描名称中以此处开头或结尾的内容。您可以使用一个内置的程序集属性,例如
CompanyName
。将此属性添加到程序集(或编辑,通常它已添加到AssemblyInfo.cs
文件中):
然后检查公司名称是否匹配:
static bool IsMyAssembly(Assembly asm) {
var company = asm.GetCustomAttribute<AssemblyCompanyAttribute>();
return company != null && company.Company == "My company";
}
静态布尔ISMY组件(组件asm){
var company=asm.GetCustomAttribute();
返回公司!=null&&company.company==“我的公司”;
}
您可以使用以下方法过滤GAC程序集:
AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.GlobalAssemblyCache)
您可以向每个程序集添加自定义属性并对其进行查询:
AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetCustomAttribute<MyAttribute>()).Where(attr => attr != null && attr.CustomProp == "MyValue")
AppDomain.CurrentDomain.GetAssemblys()
.Select(a=>a.GetCustomAttribute())。其中(attr=>attr!=null&&attr.CustomProp==“MyValue”)
你可以具体执行你的建议。声明一个公共自定义属性并将其应用于所有程序集。我将在何处声明该属性?你有一个例子吗?也许可以这样做?只需在一个新程序集中创建一个自定义属性类型,然后在所有其他程序集中引用并应用它。@MikeA,如果你付我500美元,我会告诉你如何;-)
AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetCustomAttribute<MyAttribute>()).Where(attr => attr != null && attr.CustomProp == "MyValue")