Question

I have searched the web for examples of inheriting System.Net.NetworkInformation.NetworkInterface but I cannot see any elegant solutions to this. It appears that the static method NetworkInterface.GetAllNetworkInterfaces() returns an internal implementation called SystemNetworkInterface.

Basically I want to inherit this into a class called ManagedNetworkInterface and add some additional functionality to it. Can anyone think of an elegant solution of how to do this? I have tried, but every time, something stops me dead in my tracks!

EDIT 1:

Is it acceptable to provide the implementation by casting the implementation back to NetworkInterface and then to ManagedNetworkInterface?

...this example seems messy!

ManagedNetworkInterface mni = (ManagedNetworkInterface)(NetworkInterface)NetworkInterface.GetAllNetworkInterfaces()[0];

EDIT 2:

I have also thought of using a composition-like model...

public class ManagedNetworkInterface : NetworkInterface
{
    private NetworkInterface ni;

    public override string Description
    {
        get { return ni.Description; }
    }

    //ect...
}

Is this a good or bad idea?

Was it helpful?

Solution 2

Issue resolved. I have used composition to inherit and implement abstract class NetworkInterface

Code

public class ManagedNetworkInterface : NetworkInterface
{
    private NetworkInterface ni;

    public override string Description
    {
        get { return ni.Description; }
    }

    //ect...
}

The new class now includes several mechanisms for creating instances of ManagedNetworkInterface using things like IP Address, Physical Address, ID, Name etc.

OTHER TIPS

I would write an extension method for NetworkInterface

public static class MyExtensions
{
    public static void MyMethod(this NetworkInterface ni)
    {
          //Some Code
    }
}

and use as ni.MyMethod()

class NetworkAdapter : NetworkInterface
{
    private NetworkInterface ni;

    public override string Description => _nic.Description;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top