简体   繁体   English

PCL程序集和读取资源文件

[英]PCL Assemblies and Reading Resource Files

I'm doing some reading of files in .Net that targets mobile platforms so I am using the PCL. 我正在读取.Net中针对移动平台的文件,因此我正在使用PCL。 I've noticed that if I add/change my targeted platforms my assembly options vary pretty dramatically. 我注意到,如果我添加/更改目标平台,则我的装配选项会有很大不同。 In general, what is the best way to get the max amount of assemblies in the PCL? 通常,在PCL中获得最大数量的装配的最佳方法是什么?

Here's something more specific: I'd like to use System.Reflection.Assembly.GetExecutingAssembly(); 这里有一些更具体的内容:我想使用System.Reflection.Assembly.GetExecutingAssembly(); AND System.IO.Path So far I've only been able to get one or the other. AND System.IO.Path到目前为止,我只能获得其中一个。 Has anyone found a way to get both? 有没有人找到一种同时获得两者的方法?

This is the class I want to implement: 这是我要实现的类:

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));
        }
    }
What would be the best conceptual way (code specifics OK but I don't want straight up solutions unless it's just getting scope on the right PCL assemblies) of implementing this for multiple mobile platforms? 在多个移动平台上实现此功能的最佳概念方法是什么(最好是代码细节,但我不希望直接使用解决方案,除非它只是将其限制在正确的PCL程序集上)? Specifically: IOS, Android, Windows 8.1 and Windows phone. 具体来说:IOS,Android,Windows 8.1和Windows Phone。

This is the answer to the most related SO question: 这是最相关的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. .Net 4.5(假设您的意思是完整的桌面WinForms / WPF),Windows 8和Windows Phone 8所做的文件访问非常不同,并且具有不同的文件可用。 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. 您可以做的是定义PCL可以使用的文件访问(打开,读取,保存等)接口,然后根据需要创建特定于平台的实例,然后将其传递给PCL。

URL to related SO question: C# PCL Reading from File 相关SO问题的URL: C#PCL从文件读取

Also, I like to mean what I say. 另外,我想表达我的意思。 Please tell me if I'm using any programming terminology incorrectly. 请告诉我我是否错误地使用了任何编程术语。 I'm pretty new to the software scene! 我对软件领域很陌生! Thanks guys! 多谢你们!

In general, what is the best way to get the max amount of assemblies in the PCL? 通常,在PCL中获得最大数量的装配的最佳方法是什么?

It's not really about the number of assemblies but more about the APIs you'll be able to use. 这实际上与程序集的数量无关,而是与您将能够使用的API有关。

In general, the fewer platforms your PCL targets, the more APIs you will be able to use. 通常,PCL定位的平台 ,您将能够使用的API越多 Also, the more recent the versions of all the platforms you select, the more APIs you'll get. 另外,您选择的所有平台的版本越新 ,您将获得的API越多

For fully reusable libraries, such as JSON.NET you'll probably want to target as many platforms as you can. 对于完全可重用的库(例如JSON.NET),您可能希望定位到尽可能多的平台。 However, PCL usage in applications is typically more bounded by the needs of your app. 但是,应用程序中PCL的使用通常更受应用程序需求的限制。 For the best experience, target as many platforms as you need today and include the ones you know you'll need tomorrow but don't just check all the boxes -- you'll just limit yourself. 为了获得最佳体验,请针对您今天需要的多个平台,并包括您知道明天将需要的平台,但不要只检查所有框,而要限制自己。

I'd like to use Assembly.GetExecutingAssembly() 我想使用Assembly.GetExecutingAssembly()

This API was deprecated (as was Assembly.GetCallingAssembly ). 不推荐使用此API(以及Assembly.GetCallingAssembly )。 It's not that you're missing out on much. 不是说您错过了很多东西。 In instance methods you can simply use GetType().GetTypeInfo().Assembly . 在实例方法中,您可以简单地使用GetType().GetTypeInfo().Assembly In in static methods you can replace it with typeof(TheTypeYourMethodIsIn).GetTypeInfo().Assembly . 在静态方法中,可以将其替换为typeof(TheTypeYourMethodIsIn).GetTypeInfo().Assembly

Please note GetTypeInfo() is a new method that was added in .NET 4.5. 请注意, GetTypeInfo()是.NET 4.5中添加的新方法。 On older platforms you simply omit the call to GetTypeInfo() . 在较旧的平台上,您只需忽略对GetTypeInfo()的调用。 For more details, see our blog post on Evolving the Reflection API . 有关更多详细信息,请参阅我们的博客文章Evolving Reflection API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM