在 NetStandard 1.3 中:如何获取实现接口的所有 类 以避免错误 CS0117 'Assembly' 不包含定义
In NetStandard 1.3: How I get all classes that implement an interface to avoid the Error CS0117 'Assembly' does not contain a definition
我试图在 NetStandard 1.3 class 库中使用此代码:
foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
.Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface)))) {
//do stuff
}
但是出现编译错误:
Error CS0117 'Assembly' does not contain a definition for 'GetExecutingAssembly'
我达到了,但没办法。
我知道此服务可能在 NetStandard 2 中可用,但在它发布之前(现在处于预览状态):
要对上述代码进行哪些修改以支持 NetStandard 1.3
更新:
这个问题不是重复的,我在我的问题中提到了这个 link 并说它没有帮助。
根据@mjwills 的评论,可能值得补充的是,最好避免使用 netstandard 1.5 或 1.6,因为它们与 netstandard 2.0 不兼容 https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/#div-comment-136675
我要求等效代码(不需要使用 GetExecutingAssembly):
我如何获得在 NetStandard 1.1 到 1.4 中实现接口的所有 classes?
下面的代码是NetStandard1.3中的运行,得到所有实现接口的类无编译错误:
Assembly asm = typeof(ICustomeAttribute).GetTypeInfo().Assembly; //thanks to @mjwills for his helpfull comment
var types = asm.DefinedTypes.Where(x=>x.ImplementedInterfaces.Contains(typeof(ICustomeAttribute)));
foreach (var type in types)
{
//do stuff
Console.WriteLine("class name: {0} - {1}",type.Name,type.FullName);
}
备注
摘自:Assembly.DefinedTypes Property
The DefinedTypes property is comparable to the Assembly.GetTypes
method, except that the DefinedTypes property returns a collection of
TypeInfo objects, and the Assembly.GetTypes method returns an array of
Type objects.
我试图在 NetStandard 1.3 class 库中使用此代码:
foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
.Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface)))) {
//do stuff
}
但是出现编译错误:
Error CS0117 'Assembly' does not contain a definition for 'GetExecutingAssembly'
我达到了
我知道此服务可能在 NetStandard 2 中可用,但在它发布之前(现在处于预览状态):
要对上述代码进行哪些修改以支持 NetStandard 1.3
更新:
这个问题不是重复的,我在我的问题中提到了这个 link 并说它没有帮助。
根据@mjwills 的评论,可能值得补充的是,最好避免使用 netstandard 1.5 或 1.6,因为它们与 netstandard 2.0 不兼容 https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/#div-comment-136675
我要求等效代码(不需要使用 GetExecutingAssembly):
我如何获得在 NetStandard 1.1 到 1.4 中实现接口的所有 classes?
下面的代码是NetStandard1.3中的运行,得到所有实现接口的类无编译错误:
Assembly asm = typeof(ICustomeAttribute).GetTypeInfo().Assembly; //thanks to @mjwills for his helpfull comment
var types = asm.DefinedTypes.Where(x=>x.ImplementedInterfaces.Contains(typeof(ICustomeAttribute)));
foreach (var type in types)
{
//do stuff
Console.WriteLine("class name: {0} - {1}",type.Name,type.FullName);
}
备注
摘自:Assembly.DefinedTypes Property
The DefinedTypes property is comparable to the Assembly.GetTypes method, except that the DefinedTypes property returns a collection of TypeInfo objects, and the Assembly.GetTypes method returns an array of Type objects.