我正在读取 .Net 中针对移动平台的文件,因此我使用 PCL。我注意到,如果我添加/更改我的目标平台,我的组装选项会发生很大的变化。一般来说,在 PCL 中获得最大数量的程序集的最佳方法是什么?
这里有一些更具体的内容:我想使用System.Reflection.Assembly.GetExecutingAssembly();
AND System.IO.Path
到目前为止我只能得到其中之一。有没有人找到同时获得两者的方法?
这是我想要实现的类:
public class ContentProviderImplementation : IContentProvider
{
private static Assembly _CurrentAssembly;
private Assembly CurrentAssembly
{
get
{
if (_CurrentAssembly == null)
{
_CurrentAssembly = Assembly.GetExecutingAssembly();
}
return _CurrentAssembly;
}
}
public StreamReader LoadContent(string relativePath)
{
string localXMLUrl = Path.Combine(Path.GetDirectoryName(CurrentAssembly.GetName().CodeBase), relativePath);
return new StreamReader(File.OpenRead(new Uri(localXMLUrl).LocalPath));
}
}
为多个移动平台实现此功能的最佳概念方法是什么(代码细节可以,但我不想要直接的解决方案,除非它只是在正确的 PCL 程序集上获得范围)?具体来说:IOS、Android、Windows 8.1 和 Windows 手机。
这是最相关的 SO 问题的答案:
Portable class libraries allow you to work with namespaces and classes that exist in all the platforms that you're targeting. .Net 4.5 (assuming you mean the full desktop-WinForms/WPF), Windows 8 and Windows Phone 8 all do file access very differently and have different files available to them. Where files can be accessed from also differs greatly: embedded content; embedded resources; isolated storage; shared folders; the full file system. These aren't all available on all the platforms you mention.
Short answer. You probably can't do what you're after. File system access varies dramatically across platforms and typically has to be done differently for each platform. What you can do is define an interface for file access (open, read, save, etc.) that your PCL can use and then create platform specific instances that you pass to the PCL as needed.
相关SO问题的URL:C# PCL Reading from File
此外,我喜欢言出必行。请告诉我是否错误地使用了任何编程术语。我对软件领域还很陌生!谢谢大家!
最佳答案
In general, what is the best way to get the max amount of assemblies in the PCL?
这实际上与程序集的数量无关,而更多地与您将能够使用的 API 相关。
一般来说,您的 PCL 目标平台越少,您能够使用的 API 就越多。此外,您选择的所有平台的版本越新,您将获得的 API 就越多。
对于完全可重用的库,例如 JSON.NET,您可能希望定位尽可能多的平台。然而,PCL 在应用程序中的使用通常更受应用程序需求的限制。为了获得最佳体验,请定位您今天需要的尽可能多的平台,并包括您知道明天需要的平台,但不要只选中所有复选框 - 您只会限制自己。
I'd like to use
Assembly.GetExecutingAssembly()
此 API 已被弃用(Assembly.GetCallingAssembly
也是如此)。这并不是说你错过了太多。在实例方法中,您可以简单地使用GetType().GetTypeInfo().Assembly
。在静态方法中,您可以将其替换为 typeof(TheTypeYourMethodIsIn).GetTypeInfo().Assembly
。
请注意 GetTypeInfo()
是 .NET 4.5 中添加的新方法。在较旧的平台上,您只需省略对 GetTypeInfo()
的调用即可。有关更多详细信息,请参阅我们的博客文章 Evolving the Reflection API .
关于c# - PCL 程序集和读取资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199733/