How to create two classes with the same methods, even though one of them is not`Partial`?

Asked

Viewed 175 times

1

I can create a class to implement the class System.Console, without creating extensions? Example:

'A original, da mscorlib:
Public NotInheritable Class Console
    (...)
End Class

She’s marked as a nonpartial class, so... you can’t just:

Partial Public NotInheritable Class Console
    /*.....*/
End Class

Extensions do not work because she is NotInheritable

If I try to make a clone of it? Like for example:

Public NotInheritable Class Console
    Public Sub WriteLine(ByVal arg As String)
        System.Console.WriteLine(arg) ' Irá dar uma stack overflow...
    End Sub
End Class

How can I do two Console classes? Keeping the methods?

1 answer

1


You are confusing some concepts here. The class is declared as NotInheritable means that no class can inherit from her (abstract in C#). Inheritance is a basic concept of Object-Oriented Programming and is much broader than an extension.

Partial Classes is a resource of . NET Framework and is nothing more than the separation of the same class into more than one file. In the end you have only one class, with members divided logically. This is very useful in event-based applications such as Windows Form and ASP.NET Webforms, where controls are declared in one file and the logic of handling the user interface in another, but both are the same class.

As to your question it is not possible to "extend" a class this way, but if you want to create a clone it is possible using another namespace:

Namespace Sistema
    Public Class Console
        Public Shared Sub WriteLine(ByVal arg As String)
            System.Console.WriteLine(arg)
        End Sub
    End Class
End Namespace

So you can use it like this:

Module Module1
    Sub Main()
        Sistema.Console.WriteLine("Hello World")
    End Sub
End Module

OR

If you really want to override the methods of System, then you should remove its references from the project. Create a console application and remove all references (in the project properties):

inserir a descrição da imagem aqui

Define the namespace root as System:

inserir a descrição da imagem aqui

Hence the code. Keep in mind that this way all the features of the System were lost, you will have to actually rewrite them:

Public Class Console
    Public Shared Sub WriteLine(ByVal arg As String)
        ' código da sua implementação de escrita no console
    End Sub
End Class
  • Yeah, that’s not what I wanted, I’m trying to create a module to get into the place of class System.Console, using Win32native libs. but it’s not working, giving Ambigous.

  • I updated my answer, see if you understand and answer.

  • It worked though, if any app is going to use my Assembly, you’ll have to remove one of the references to use my reference....

  • Exactly, but it’s something that makes perfect sense, since you’re rewriting the namespace.

  • Yes, I am creating an expansion project for the namespace System, and I want to put a method in the class "Console", I can’t use extensions, ta difficult...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.