简体   繁体   English

IsAssignableFrom() 在应该返回 true 时返回 false

[英]IsAssignableFrom() returns false when it should return true

I am working on a plugin system that loads .dll's contained in a specified folder.我正在开发一个插件系统,它加载包含在指定文件夹中的 .dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后我使用反射来加载程序集,遍历它们包含的类型并识别任何实现我的IPlugin接口的类型。

I am checking this with code similar to the following:我正在使用类似于以下的代码进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.出于某种原因,IsAssignableFrom() 在应该返回 true 时一直返回 false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我尝试通过显式地给它一个应该通过的类型来替换t ,它工作正常,但由于某种原因,它不适用于从加载的程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.为了让事情变得更奇怪,代码在我同事的机器上运行良好,但在我的机器上却不行。

Does anyone know of anything that might cause this sort of behavior?有谁知道可能导致这种行为的任何事情?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

I had a similar issue which was caused due to dependency injection into plugins.我有一个类似的问题,这是由于插件的依赖注入引起的。 Assume that assembly contains a definition of MyConcreteClass which implements ISomeInterface.假设程序集包含实现 ISomeInterface 的 MyConcreteClass 的定义。 MyConcreteClass constructor requires the dependencies I added to the serviceProvider. MyConcreteClass 构造函数需要我添加到 serviceProvider 的依赖项。 I am trying to instantiate the object using我正在尝试使用实例化对象

 using Microsoft.Extensions.DependencyInjection;

 private IEnumerable<ISomeInterface> CreatePlugins(Assembly assembly)
 {
        ServiceCollection serviceCollestion = new ServiceCollection();
        serviceCollestion.AddSingleton(dependency1);
        serviceCollestion.AddSingleton(dependency2);
        serviceCollestion.AddSingleton(dependency3);
        var serviceProvider = serviceCollestion.BuildServiceProvider();

        foreach (Type type in assembly.GetTypes())
        {
            if (typeof(My.Namespace.ISomeInterface).IsAssignableFrom(type))
            {
                ISomeInterface result = ActivatorUtilities.CreateInstance(serviceProvider, type) as ISomeInterface;
            }
        }
 }

I got the exception:我得到了例外:

Unable to resolve service for type 'My.Namespace.ISomeInterface' while attempting to activate 'Another.Namespace.MyConcreteClass'.尝试激活“Another.Namespace.MyConcreteClass”时无法解析“My.Namespace.ISomeInterface”类型的服务。 at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider 提供程序)在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider 提供程序,类型 instanceType,对象 [] 参数)

I then found out that the process which called ActivatorUtilities.CreateInstance was already referencing a dependency of MyConcreteClass and that's why the instantiation failed.然后我发现调用 ActivatorUtilities.CreateInstance 的进程已经引用了 MyConcreteClass 的依赖项,这就是实例化失败的原因。 In order resolve this I followed the advice from the link below and cleared the folder of Another.Namespace from any assembly referenced by my process prior to loading it.为了解决这个问题,我遵循下面链接中的建议,并在加载之前从我的进程引用的任何程序集中清除了 Another.Namespace 的文件夹。

.NET: Unable to cast object to interface it implements .NET:无法将对象转换为它实现的接口

I am working on a plugin system that loads .dll's contained in a specified folder.我正在一个插件系统上加载指定文件夹中包含的.dll。 I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.然后,我使用反射来加载程序集,遍历它们所包含的类型,并标识实现我的IPlugin接口的任何程序集。

I am checking this with code similar to the following:我正在使用类似于以下代码的方式对此进行检查:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

For some reason IsAssignableFrom() keeps returning false when it should be returning true.由于某些原因,IsAssignableFrom()在应返回true时始终返回false。 I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly.我试图通过显式地给它赋予应该传递的类型来替换t ,并且它可以正常工作,但是由于某些原因,它不适用于从已加载程序集返回的类型。 To make things stranger, the code works fine on my co-worker's machine but not on mine.使事情变得奇怪的是,该代码在我的同事的机器上可以正常工作,但在我的机器上却无法正常工作。

Does anyone know of anything that might cause this sort of behavior?有谁知道任何可能导致这种行为的信息?

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM