Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override base classes\structs such as int, string?

To reduce downvotes: (Skippable at first)

I am aware that this question sounds pointless and\or weird. I am creating JIT that takes C# code compiles it with csc.exe, extracts the IL and parallize it into CUDA, and I want to override some of the things in C#.

How override base things such as int\ string? I tried:

class String { /* In namespace System of course */
    BasicString wrap_to;
    public String( ) {
        wrap_to = new BasicString( 32 ); // capacity
    }
    public String( int Count ) {
        wrap_to = new BasicString( Count );
    }
    public char this[ int index ] {
        get { return wrap_to[ index ]; }
        set {
            if ( wrap_to.Handlers != 1 )
                wrap_to = new BasicString( wrap_to );
            wrap_to[ index ] = value;
        }
    }
    ...
}
... // not in namespace System now
class Test {
    public static void f(string s) { }
}

But when I tried:

Test.f( new string( ) );

It error'd Cannot implicitly convert type 'System.String' to 'string'. I tried moving my System.String to just string in the global scope and it error'd on the class itself. Any ideas how? I think somehow it could help if I can compile my .cs files without that mscorlib.dll, but I can't find a way to do it.

Even a way to reach to csc.exe source code may help. (This is critical.)

like image 740
LyingOnTheSky Avatar asked Apr 13 '15 19:04

LyingOnTheSky


People also ask

How to override a base class method in Java?

Learn how to override a base class method by following these 10 steps. Open your text editor and type in the following Java statements: The program provides a howPaid method that returns a string specifying how a person is paid. The program also defines two properties: firstName and lastName.

How do I override the toString method in a struct?

To override the ToString method in your class or struct: Declare a ToString method with the following modifiers and return type: Implement the method so that it returns a string. The following example returns the name of the class in addition to the data specific to a particular instance of the class.

Can We override the base class payment method in the derived class?

In the Employee class, however, we can be more specific; e.g., "Employee is paid semi-monthly by W-2". Accordingly, in the derived class, we would desire to override the base class payment method.

How to implement overriding in C#?

Below is an example of how we can implement overriding in C#. In the above example there are two classes, one is base class or parent class and the other is derived class or we can say, child class. A base class method is derived in child class. In this, method in a parent is virtual which means it can be overridden by the child class.


1 Answers

Yes, you will have to not reference mscorlib.dll. Otherwise there will be two System.String classes, and clearly the predefined (C# Specification given) type string cannot be both of them.

See /nostdlib C# compiler option. The page also describes how to do it with a setting in the Visual Studio IDE.

You need to write really many other required types (or copy-paste them) when you do not refer mscorlib.dll!

like image 169
Jeppe Stig Nielsen Avatar answered Oct 30 '22 23:10

Jeppe Stig Nielsen