Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get types that implement an interface

Discussion in 'VR' started by Florian-Nouviale, Feb 23, 2017.

  1. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    54
    Hi,

    Also my question might not seem to be hololens related, I think this is where I might find the help I need.
    In my application, I have some code that finds all the types implementing an interface, some kind of plugin approach where a user can define new implementation in Unity or in an external assembly.
    I use System.AppDomain.CurrentDomain.GetAssemblies() to get all loaded assemblies (meaning all assemblies in the unity project)
    and from there I access all the types and check if they implement the interface I'm interested in.
    My problem is that UWP has no System.AppDomain class and I'm looking for an alternative to do the same thing.
    I've been looking on google for days now and only found solutions (related to .net core) that can't be used with the hololens (missing classes when building the projet in unity).

    So my hope is that someone here ran to the same issue and has a solution (or maybe some informations on an upcoming change in the hololens build ?)
     
  2. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Hopefully this helps.

    you could try this:
    var assembly = this.GetType().GetTypeInfo().Assembly;

    or:

    public static async Task<List<Assembly>> GetAssemblyList()
    {
    List<Assembly> assemblies = new List<Assembly>();

    var files = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
    if (files == null)
    return assemblies;

    foreach (var file in files.Where(file => file.FileType == ".dll" || file.FileType == ".exe"))
    {
    try
    {
    assemblies.Add(Assembly.Load(new AssemblyName(file.DisplayName)));
    }
    catch (Exception ex)
    {
    System.Diagnostics.Debug.WriteLine(ex.Message);
    }

    }

    return assemblies;
    }

    Then to loop through them you could do:

    foreach (var assembly in GetAssemblyList().Result)
    {
    //Do something with it
    }

    Link:
    http://stackoverflow.com/questions/32487141/get-list-of-loaded-assemblies-on-uap10-platform
     
  3. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    54
    Thank you !

    That's working! Also it seems it's loading a lot of assemblies in the process but I guess it's ok.
    I just ran into a minor issue that when I add an assembly to my unity project, it isn't added as a references assembly in the generated visual studio solution when building for hololens (when the visual studio already existed previously)
     
  4. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Glad to have helped :)
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    Noooo, that will not work! Managed DLLs will not be present on disk in master builds, since they get compiled down to native code. Unfortunately I don't think there is a System.AppDomain.CurrentDomain.GetAssemblies() equivalent on .NET scripting backend.
     
  6. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    54
    You mean on the hololens it won't work ?
    It works as expected in the emulator so far. I haven't tried on the real device yet :)
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    I meant when you switch to master configuration, which is required for submitting applications to the windows store.
     
  8. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    54
    Ok, we do not plan to submit any application right now, it's for research purpose only. But I'll keep that in mind if we do need to submit someday.
    Some ideas :
    One solution would be to detect interesting types before the build and generate some code to make the type subscribe to the interested class.
    Another one would be to force people to implement a "plugin" interface to perform this registration in their assembly.

    Thanks for the information !