有没有便携式图书馆AppDomain.GetAssemblies的方法吗? [英] Is there an alternative to AppDomain.GetAssemblies on portable library?

查看:167
本文介绍了有没有便携式图书馆AppDomain.GetAssemblies的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来获得一个便携式库项目内的当前应用程序的组件

I'm looking for a way to get the current app's assemblies inside a portable library project.

在经典的库项目,下面的代码行做的工作:

In classic library project, the code line below do the job:

var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();



但似乎System.AppDomain不可用于移植库。

But seems that System.AppDomain is not available to portable library.

有谁知道一种方式来获得移植库在当前域组件?

Does anyone know a way to get the current domain assemblies on portable library?

推荐答案

您可以使用台钩:

在您的便携式库:

using System.Collections.Generic;

namespace PCL {
  public interface IAppDomain {
    IList<IAssembly> GetAssemblies();
  }

  public interface IAssembly {
    string GetName();
  }

  public class AppDomainWrapper {
    public static IAppDomain Instance { get; set; }
  }
}

和您可以访问它们(在您的便携式库),如:

and you can access them (in your portable library) like:

AppDomainWrapper.Instance.GetAssemblies();

在您的依赖于平台的应用程序,您需要实现它:

In your platform-dependent application you'll need to implement it:

public class AppDomainWrapperInstance : IAppDomain {
  IList<IAssembly> GetAssemblies() {
    var result = new List<IAssembly>();
    foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies()) {
      result.Add(new AssemblyWrapper(assembly));
    }
    return result;
  }
}

public class AssemblyWrapper : IAssembly {
  private Assembly m_Assembly;
  public AssemblyWrapper(Assembly assembly) {
    m_Assembly = assembly;
  }

  public string GetName() {
    return m_Assembly.GetName().ToString();
  }
}

和引导它

AppDomainWrapper.Instance = new AppDomainWrapperInstance();

这篇关于有没有便携式图书馆AppDomain.GetAssemblies的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆