uwp - How to get Application Name and Version in a Portable Class Library? -
i wondering how product name or product version in portable class library. far have tried these below:
system.reflection.assembly.getexecutingassembly().getname().version;
the error here getexecutingassembly() not found.
appdomain.currentdomain.domainmanager.entryassembly.getname().version
the error here appdomain not found.
public static string version { { var assembly = typeof(mytype).gettypeinfo().assembly; // in pcl profiles above line is: var assembly = typeof(mytype).assembly; var assemblyname = new assemblyname(assembly.fullname); return assemblyname.version.major + "." + assemblyname.version.minor; } }
the problem 1 gives version of pcl, , not app's version.
thanks all.
everything have written true. far know, there no common way across supported platforms reference entry assembly of application, , there no common api available in portable class libraries.
however, doesn't stop getting ahold of information inside portable class library. need abstract away functionality , take advantage of dependency injection provide correct implementation each platform.
your portable class library implement common interface:
public interface iproductinfoprovider { string getversion(); }
for full .net framework implement this:
public class productinfoprovider : iproductinfoprovider { public version getversion() { return assembly.getexecutingassembly().getname().version; } }
the class needs version information accept instance of iproductinfoprovider
constructor parameter, allowing called whenever necessary:
public class myclass { private readonly iproductinfoprovider _productinfoprovider; public myclass(iproductinfoprovider productinfoprovider) { _productinfoprovider = productinfoprovider; } }
when instantiating class pass correct implementation of interface:
var myinstance = new myclass(new productinfoprovider());
since might want instantiate class inside portable class library, use dependency injection framework, such ninject define correct implementation in platform specific application initialization:
public class applicationmodule : ninjectmodule { public override void load() { this.bind<iproductinfoprovider>().to<productinfoprovider>(); } }
this allow ninject create new instance of myclass
correct implementation of iproductinfoprovider
injected.
Comments
Post a Comment