C # - overriding somes classes in the System namespace

I have a project that needs to be compiled in Compact.NET Framework 3.5 and .NET Framework 3.5 (actually 2 projects, just compiling the parameters that change between them). A.

The problem is that some classes are missing in CF.NET, so I created it manually (and implemented all the class members available in .NET

One example: FtpWebRequest / FtpWebResponse classes.

It's nice to write something like this (if so, why?):

#if CFNET35 // Only if we are in Compact Framework 3.5 mode namespace System.Net { public class FtpWebRequest : WebRequest { // ... } public class FtpWebResponse : WebResponse { // ... } } #endif 

I am sure that in CF.NET35 these methods will never be available, so can I write it?

I would write that to avoid name collisions when using my library in projects.

This allows me in other projects to always use using System.Net; don't ask me what infrastructure I use ...

Thanks!


EDIT

A few months later I had to evaluate the strategy that I used.

As I said, I redefine the System (.Net) namespace by performing conditional compilation, so I have two DLLs (one for CF.NET, one for .NET).

It also means that all my applications using this DLL are doubled (each time a CF.NET application and one .NET application that includes the corresponding library).

So, it was a bad idea, I have many projects twice, and this is superfluous in that the .NET application can directly include and use the CF.NET library.

Also, something that I didn't care about was that the .NET application included a CF.NET library with a redundant System namespace, which initialization would fail due to a class name collision ...

So EPIC FAIL , providing a common interface is the best way to manage this case.

+4
source share
4 answers

I would probably avoid using System namespaces to write my own code. I have seen open source libraries trying to do this, and usually this leads to a headache.

Perhaps you better create an interface that will be split between a complete and compact infrastructure, and then implement the interface in its entirety and a CF that provides the necessary functionality using the built-in System classes or classes you write yourself.

This may seem redundant, but in the future you will be safer if something in System.Net changes. Your call code should just refer to the interface, and you can connect any implementation depending on which platform you are on.

 // Shared interface public interface IFtpUtil { SomeFileObject GetFile(SomeArgument a); void PutFile(SomeFileObject f, SomeArgument a); } // Full framework implementation public class FullFtpUtil : IFtpUtil { public ... GetFile(...) { // Use System.Net classes from full framework } public ... PutFile(...) { // Use System.Net classes from full framework } } // Compact framework implementation public class CompactFtpUtil : IFtpUtil { public ... GetFile(...) { // Use your own FTP classes } public ... PutFile(...) { // Use your own FTP classes } } 
+6
source

I would not do that; not for any specific technical reason, but because it can cause some serious confusion. No one expects custom classes to be in the System namespace.

Classes in the System namespace have been widely tested and used by many people, so if someone from your team uses System.Net.FtpWebRequest in their code and their code does not work, they will (and should) look for errors in their own code. After many hours of searching, they will get angry with you when they find out that the error was in an explicitly built-in system class.

+5
source

Had a similar case. Want to use some namespaces / classes in the winforms structure, but removed from the framework for mobile devices.

I suggest not putting your namespaces in the "system" namespace, but I suggest creating your code as similar to the framework as possible except for your application. Requires special additional features.

 using system; // "sysmobile" emulates code for the "system" namespace namespace sysmobile { class FtpWebRequest { ... } class FtpWebResponse { ... } } 
0
source

I understand that I was a little late for this question, but I think I can add some value, since we do it a lot. The SDF probably accounts for 50% of the fillings for classes that match the desktop structure. We used our own namespace. So let's look at Ftp as an example. We will do the following:

 namespace OpenNETCF.Net public class FtpWebRequest { ... } 

The consumer application will use aliases for this:

 #if NETCF using FtpWebRequest = OpenNETCF.Net.FtpWebRequest; #else using FtpWebRequest = System.Net.FtpWebRequest; #endif void Foo() { // the alias above resolves these for you // enabling a single, fairly clean code base var request = new FtpWebRequest(...); } 
0
source

Source: https://habr.com/ru/post/1343985/


All Articles