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

To reduce downvotes: (you can skip first)

I know this question sounds pointless and / or strange. I am creating a JIT that accepts C # code that compiles it with csc.exe, extracts IL and blocks it in CUDA, and I want to override some of the things in C #.

How to override basic things like 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( ) ); 

This is a mistake Cannot implicitly convert type 'System.String' to 'string' . I tried to move my System.String only string to the global scope, and this is an error in the class itself. Any ideas how? I think this can help if I can compile my .cs files without this mscorlib.dll , but I cannot find a way to do this.

Perhaps even access to the csc.exe source code will help you. (It is very important.)

+5
source share
2 answers

Yes, you do not need to reference mscorlib.dll . Otherwise, there will be two classes System.String , and, obviously, the predefined (specified by the C # specification) type string cannot be both.

See /nostdlib C # Compiler . The page also describes how to do this with customization in the Visual Studio IDE.

You need to write really many other required types (or copy them) if you are not referencing mscorlib.dll !

+2
source

Perhaps even access to the csc.exe source code will help you. (It is very important.)

Judging by your comments, this is really the bit you really need. (Attempting to change int and string will involve changing mscorlib and almost certainly CLR. Ouch.)

Fortunately, you're in luck: Microsoft has the open-source Roslyn , the next-generation C # compiler that will ship with Visual Studio 2015.

If you want to change the behavior of the compiler, you can unlock it and change it accordingly. If you just need to get an abstract syntax tree (and the like), then you can do it without changing the compiler at all - Roslyn is a design that is the β€œcompiler API”, and not just a black box that takes the source of the code and spits out IL. (As a sign of how rich the API is, Visual Studio 2015 uses a public API for everything - Intellisense, refactoring, etc.)

+2
source

All Articles