开发者

C# - Overriding somes classes available in System namespace

开发者 https://www.devze.com 2023-02-17 01:45 出处:网络
I\'ve a project that need to be compiled in Compact .NET Framework 3.5 and .NET Framework 3.5 (2 projects in fact, just compiling options that changes between both).

I've a project that need to be compiled in Compact .NET Framework 3.5 and .NET Framework 3.5 (2 projects in fact, just compiling options that changes between both).

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

One example : The FtpWebRequest / FtpWebResponse classes.

It's bad to write something like this (if yes, 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'm sure that in CF.NET35 these methods will never be available, so can I write it?

I would write that in order to avoid a name collision when using the my library in projects.

It allows me in other projects always using System.Net; whitout asking me which framework I use...

Thanks !


EDIT

Few months later, I'd to assess the开发者_开发百科 strategy I used.

As said, I override System(.Net) namespace by doing conditional compilation, so I've two DLL's (one for CF.NET, one for .NET)

That includes too that all my applications using this DLL are in double (each a time a CF.NET app and one .NET app that includes the corresponding library).

So, was a bad idea, I've a lot of project in double and that's unnecessary in the way that a .NET app can directly include and use a CF.NET library.

Moreover, something I haven't take care in consideration is if that a .NET app include a CF.NET library with an overriden System namespace, initialization of it will fail because of a class name collision...

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


I would probably avoid using the System namespaces for writing custom code. I've seen open source libraries that try to do that, and it usually results in a headache.

You might be better off creating an interface that is shared between the full and compact framework, then implement the interface in the full and CF that provide the functionality you need using built-in System classes, or classes you write yourself.

This might seem like overkill, but you'll be safer in the future if something in System.Net changes. Your calling code should just reference the interface, and you can plug in either implementation depending on what platform you're 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
    }
}


I wouldn't do this; not because of any specific technical reason, but because it could potentially cause some major confusion. No one expects custom classes to reside in the System namespace.

The classes in the System namespace have been extensively tested and used by many people, so if someone else in your team uses System.Net.FtpWebRequest in their code and their code isn't working, they will (and should) search for bugs in their own code first. After many hours of searching, they would be angry with you when they find out that the error was in the apparently built-in system class.


I realize that I'm way late to this question, but I think I can add some value since we do this a lot. The SDF is probably 50% fill-ins for classes that match the desktop framework. What we did was used our own namespace. So let's look at Ftp as an example. We'd do this:

namespace OpenNETCF.Net

public class FtpWebRequest  { ... }

The consumer app would use aliasing to work this out:

#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(...);
}


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

I suggest not to put your namespaces in the "system" namespace, but I suggest to design your code, as similar as possible to the framework, unless your app. requires specific additional functionality.

using system;

// "sysmobile" emulates code for the "system" namespace

namespace sysmobile {
  class FtpWebRequest { ... }
  class FtpWebResponse { ... }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号