List all DLL's implementing a specific interface from the GAC

后端 未结 5 1816
借酒劲吻你
借酒劲吻你 2021-01-21 09:49

Aloha

Given a plug-in architecture (C# / .NET 3.5) with the plug-ins stored in the GAC, how can I list/load all dll\'s that contain types that implement my specific inte

5条回答
  •  后悔当初
    2021-01-21 10:25

    First off, I'd recommend not doing this. To do this, you have to load all the assemblies from the GAC. I'd recommend you have your user (or an admin, or whatever) tell you what assemblies to try to load from (though for that, you might want a list of all the options, which might be why you're asking this...)

    That said, this might work, but it's throwing errors for several assemblies it should work for, and I'm not sure why.... Also, I'm not sure how to detect where the GAC is -- c:\windows\assembly is the default, but I don't know where the real value is stored (registry?)

    var targetType = typeof(IComparable);
    var errors = new List();
    var c = Directory.GetFiles(@"c:\windows\assembly", "*.dll", SearchOption.AllDirectories).ToList()
        .ConvertAll(f => Path.GetFileNameWithoutExtension(f))
        .Where(f => !f.EndsWith(".ni"))
        .Distinct().ToList()
        .ConvertAll(f => { try { return Assembly.ReflectionOnlyLoad(f); } catch (Exception ex) { errors.Add(ex); return null; } })
        .Where(a => a != null)
        .SelectMany(a => { try { return a.GetTypes(); } catch (Exception ex) { errors.Add(ex); return new Type[] { }; } })
        .Where(t => targetType.IsAssignableFrom(t));
    

    Good luck with that.

提交回复
热议问题