© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
10 replies
the1mason

typeof(T).IsAssignableFrom does not work as I expected

There is a method in a generic class
PluginLoader<TPlugin> : IPluginLoader<TPlugin>
PluginLoader<TPlugin> : IPluginLoader<TPlugin>
:
    private static TPlugin? CreatePlugin(Assembly assembly)
    {
        var types = assembly.GetTypes().Where(t => typeof(TPlugin).IsAssignableFrom(t)).ToArray();
        return types.Length switch
        {
            > 1 => throw new ApplicationException(
                $"Found more than 1 type, implementing IPlugin in assembly {assembly.FullName}"),
            < 1 => throw new ApplicationException(
                $"Found 0 types, implementing IPlugin in assembly {assembly.FullName}"),
            _ => (TPlugin?)Activator.CreateInstance(types.First()) ?? default
        };
    }
    private static TPlugin? CreatePlugin(Assembly assembly)
    {
        var types = assembly.GetTypes().Where(t => typeof(TPlugin).IsAssignableFrom(t)).ToArray();
        return types.Length switch
        {
            > 1 => throw new ApplicationException(
                $"Found more than 1 type, implementing IPlugin in assembly {assembly.FullName}"),
            < 1 => throw new ApplicationException(
                $"Found 0 types, implementing IPlugin in assembly {assembly.FullName}"),
            _ => (TPlugin?)Activator.CreateInstance(types.First()) ?? default
        };
    }


There is also an interface
IWebPlugin
IWebPlugin
with implementation
Plugin1
Plugin1

This method is called with an assembly, containing
Plugin1
Plugin1
and tries to create an instance of it. It worked before I made PluginLoader generic, but now
typeof(TPlugin).IsAssignableFrom(t)
typeof(TPlugin).IsAssignableFrom(t)
returns false.
Plugin1.GetInterface("IWebPlugin")
Plugin1.GetInterface("IWebPlugin")
returns
IWebPlugin
IWebPlugin
,
typeof(TPlugin
typeof(TPlugin
gets
IWebPlugin
IWebPlugin
too, but the original condition fails nevertheless.

Here's my result of playing with the debugger
 var debugTypes = assembly.GetTypes()
  {System.RuntimeType[1]}
    [0]: {Plugin1.Plugin1}

  var iWebPlugin = debugTypes[0].GetInterface("IWebPlugin")
  {RainCrab.Plugins.AspNet.IWebPlugin}


  var iaf = iWebPlugin.IsAssignableFrom(debugTypes[0])
  true

  var iafGeneric = typeof(TPlugin).IsAssignableFrom(debugTypes[0])
  false

  typeof(TPlugin)
  {RainCrab.Plugins.AspNet.IWebPlugin}
 var debugTypes = assembly.GetTypes()
  {System.RuntimeType[1]}
    [0]: {Plugin1.Plugin1}

  var iWebPlugin = debugTypes[0].GetInterface("IWebPlugin")
  {RainCrab.Plugins.AspNet.IWebPlugin}


  var iaf = iWebPlugin.IsAssignableFrom(debugTypes[0])
  true

  var iafGeneric = typeof(TPlugin).IsAssignableFrom(debugTypes[0])
  false

  typeof(TPlugin)
  {RainCrab.Plugins.AspNet.IWebPlugin}


Why if Plugin1 implements IWebPlugin and typeof(TPlugin) is IWebPlugin, Plugin1 is not assignable from IWebPlugin?

C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

UDP Multicast does not work as expected [Answered]
C#CC# / help
4y ago
✅ NotNullWhen not working as expected
C#CC# / help
3y ago
Visual Studio doesn't work as expected with source generators [Answered]
C#CC# / help
4y ago
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
C#CC# / help
3y ago