简体   繁体   中英

Using C# 4.0 library in Windows Store App

I have C# .NET4 library project I want to use in Windows Store App. As I can't reference this DLL directly, because of the not supported .NET version, I was thinking of writing Windows Store App Library, which will be wrapper around calls to the C# (.NET4) library. Any hints on how to achieve this?

SOS

Many thanks!

It isn't about just wrapping and referencing a library, it is about a lack of some of the basic types, methods, extensions, etc. within the Windows Store App version of the runtime / framework that will prohibit you from just wrapping / linking a full .NET 4 library assembly. You won't be able to build/link it for a platform that it isn't compatible with, and even if you could build it, it simply wont execute on that platform's CLR. Since you have to create a specific Windows App project, Visual Studio adds the references to the particular .NET framework to your project, and at compile time, you wouldn't even be able to build the project because the .NET 4 assembly would be referencing dependencies that are not in the project, nor compatible with the runtime target.

What you are suggesting (wrapping) is just like me saying "I have this Windows library, I want to use on Linux, can I wrap the calls in a layer and then deploy on Linux?" and the answer would be "no without either (1) emulation, or (2) reimplementation". Sure, the CLR / .NET bytecode is more compatible than Linux vs Windows, but the concept is still the same. One version of the CLR/Framework != another version. There are types / library calls that are just plain missing on the target runtime environment.

You will instead need to probably create an alternative implementation of the library in question.

The best I can suggest is to use a tool like Reflector or dotPeek to decompile the .NET4 assembly, and see how much of it you can paste into a portable or Windows Store App project. Keep in mind that if this is a 3rd party commercial library, you may be in violation of the license agreement, that is up to you.

Use this guide to replace the missing parts with equivalents for that platform: http://msdn.microsoft.com/library/windows/apps/br230302.aspx#general

As it says in the guide, you can also create a Portable Class Library project to develop a .NET Framework library that can be used from a Windows Store app but you are still limited to the subset of the API that is portable. If you take the approach above with Reflector, you may actually find out that the .NET 4 assembly isn't using any significant non-portable framework types/references and can be easily recompiled. On the other hand, it could be the opposite case just as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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