I set my mind on creating a lib that i could use for different projects, including .net 4.5 console apps, windows store apps, windows phone 8. My lib are doing some socket programming, and if it was normal 4.5 i would use the TcpClient.
The first(Assumed) step would be to create a Portable Lib, but this ends quickly as TcpClient are not supported there.
What are the normal approach for creating such libraries? What would be my firsts step for creating a lib i can use for multiply targets?
Very well known issue.
There are couple of ways that you can get around that problem.
Shared code files should be added a links in the relevant project. that way you can change is once and it will affect all the dependent projects. keep in mind that of course rebuilding all dependent project is a must in that case.
Right-click the dependent project, select Add->Existing Item – in the dialog form, drop down the Add button and select Add As Link.
You will have to use some C# Preprocessor Directives for distingushing between different platforms – for example, some namespaces and APIs could be different in Windows 8.
Use partial classes and inheritance for extending types.
for example, let say that you have a type that one of it’s methods should download a file from the internet and save it locally.
Let’s assume that in Windows 8 and Windows Phone the API are different (they are actually), you can create the type with the private method deceleration in a common file, and create partial class that implement that method in every relevant project – one partial class for Windows Phone, and one partial class in Windows 8 and each will be built in it relevant context.
For more information about partial classes and methods, read here.
I hope that helps.
Anyway, specifically TcpClient that you’ve mentioned you want to use – If I remember correctly it is not supported in Windows Phone as all and not just in Portable Library 🙂