I'm doing some reading of files in .Net that targets mobile platforms so I am using the 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?
Here's something more specific: I'd like to use System.Reflection.Assembly.GetExecutingAssembly();
AND System.IO.Path
So far I've only been able to get one or the other. 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? Specifically: IOS, Android, Windows 8.1 and Windows phone.
This is the answer to the most related SO question:
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.
URL to related SO question: C# PCL Reading from File
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?
It's not really about the number of assemblies but more about the APIs you'll be able to use.
In general, the fewer platforms your PCL targets, the more APIs you will be able to use. Also, the more recent the versions of all the platforms you select, the more APIs you'll get.
For fully reusable libraries, such as JSON.NET you'll probably want to target as many platforms as you can. However, PCL usage in applications is typically more bounded by the needs of your app. 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()
This API was deprecated (as was Assembly.GetCallingAssembly
). It's not that you're missing out on much. In instance methods you can simply use GetType().GetTypeInfo().Assembly
. In in static methods you can replace it with typeof(TheTypeYourMethodIsIn).GetTypeInfo().Assembly
.
Please note GetTypeInfo()
is a new method that was added in .NET 4.5. On older platforms you simply omit the call to GetTypeInfo()
. For more details, see our blog post on Evolving the Reflection API.