locked
AppDomain / Assembly Enumeration RRS feed

  • Question

  • I understand that Metro/WinRT apps have a single appdomain, that's fine, but the removal of the AppDomain object seems to have also removed the ability to enumerate which assemblies are loaded into the appdomain.  Enumerating through all of the loaded assemblies is something I need to do and, as far as I can tell, isn't in "violation" of any of the rules, but there doesn't seem to be any way to do it?

    Is this an oversight, am I missing another class/method somewhere, or is this never coming back?

    Wednesday, September 21, 2011 1:30 PM

Answers

  • There currently isn't any way to get the assemblies loaded in the AppDomain however for Metro style apps you are most likely only interested in the assemblies in the pacakge so you could do something like:

    private async Task<IEnumerable<Assembly>> GetAssemblyListAsync()
    {
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    
        List<Assembly> assemblies = new List<Assembly>();
        foreach (StorageFile file in await folder.GetFilesAsync())
        {
            if (file.FileType == ".dll" || file.FileType == ".exe")
            {
                AssemblyName name = new AssemblyName() { Name = file.Name };
                Assembly asm = Assembly.Load(name);
                assemblies.Add(asm);
            }
        }
    
        return assemblies;
    }
    
    


    Blog: http://weblogs.asp.net/whaggard
    Thursday, September 22, 2011 7:11 AM

All replies

  • Hi,

          I am going to try to get an answer to your question.  What's your scenario here?  Are you sure this application is a good fit for a Metro-style application?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Wednesday, September 21, 2011 7:34 PM
    Moderator
  • Thanks for the reply - it's a library rather than an app - I've had a request from a user to get my IoC container (TinyIoC) working in WinRT so it can be used to build Metro apps. One of the "main" features it has is 0 setup - it scans all your types and wires things up automatically.  It already works on Windows (obviously :-)), PocketPC, Windows Phone 7, Silverlight, MonoTouch and MonoDroid - so getting it to work on WinRT/Metro would help people porting things / sharing code amongst those platforms.
    Wednesday, September 21, 2011 7:39 PM
  • There currently isn't any way to get the assemblies loaded in the AppDomain however for Metro style apps you are most likely only interested in the assemblies in the pacakge so you could do something like:

    private async Task<IEnumerable<Assembly>> GetAssemblyListAsync()
    {
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    
        List<Assembly> assemblies = new List<Assembly>();
        foreach (StorageFile file in await folder.GetFilesAsync())
        {
            if (file.FileType == ".dll" || file.FileType == ".exe")
            {
                AssemblyName name = new AssemblyName() { Name = file.Name };
                Assembly asm = Assembly.Load(name);
                assemblies.Add(asm);
            }
        }
    
        return assemblies;
    }
    
    


    Blog: http://weblogs.asp.net/whaggard
    Thursday, September 22, 2011 7:11 AM
  • Thanks Wes, that's a little long-winded but I'll give it a whirl :-)

    I'll probably create my own private AppDomain object and put that code inside to replicate the "normal" .net syntax, just in case this is fixed further down the line.

    Now.. back to adding .GetTypeInfo() all over the place ;-)

    Thursday, September 22, 2011 7:41 AM
  • I've dropped this into an AppDomain class should anyone else want to use it for "compatibility":

    https://gist.github.com/1234767

    Thursday, September 22, 2011 1:29 PM
  • Just a thought, but in the other platforms it seems like pulling from the AppDomain to get the currently loaded assembies seems like the wrong approach. This would mean that every assembly that you want to participate in DI would need to be referenced by the entry assembly and also have a type used/referred in the startup path.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    Thursday, September 22, 2011 3:32 PM
  • Just a thought, but in the other platforms it seems like pulling from the AppDomain to get the currently loaded assembies seems like the wrong approach. This would mean that every assembly that you want to participate in DI would need to be referenced by the entry assembly and also have a type used/referred in the startup path.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com

    Sure, this is only one of many ways to set things up, and something that projects of any complexity will outgrow. I do intend at some point to pull the assembly loading code we have in Nancy into TinyIoC, but I haven't yet because I think it will be a bit of a cross platform nightmare :-)

    My current goal is just to get as close as I can to parity on current features for WinRT, just so people can use it.

    Friday, September 23, 2011 8:08 AM
  • For anyone that is trying to accomplish this, it looks like with the consumer preview bits you need to strip out the file extension...

                            var filename = file.Name.Substring(0, file.Name.Length - file.FileType.Length);
                            AssemblyName name = new AssemblyName() { Name = filename };
    -- Tim
    Tuesday, March 6, 2012 2:43 AM
  • Is this cluster of a problem still in effect with Universal Apps?  Or is this still the best way of figuring out something in the "latest and greatest" (but more like just plain late and grating) stack that has been around since .NET 1.0? :P
    Sunday, March 8, 2015 2:49 PM
  • What's the point in replying to a 4-year old thread? if you have a question, please create a new post.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, March 9, 2015 4:29 PM
    Moderator
  • I do have a question, and I asked it.  The point in asking it here is that I doubt much has changed with this cluster of a problem in the 4-years since it was discussed, and I am looking to be corrected (but knowing the history of MSFT and its outstanding practice of fragmenting its own framework as much as humanly possible, I am betting I have this right... but again, who knows!).  And besides, my caustic snark/drivel won't carry as far in a new thread. :P

    However, after looking into this more, I do stand corrected on one thing... GetAssemblies has been around since 1.1, not 1.0. :P

    Monday, March 9, 2015 4:40 PM