tags:

views:

721

answers:

7

I'm currently working to hack my .NET operating system into running on top of MS.NET/Mono (previously it ran bare-metal only), and I've hit a small snag. I need to implement my own System.Console that talks to my service infrastructure, and I can't find a good way to replace it without either 1) not linking against mscorlib (would be hellacious to do), or 2) Using a NotQuiteSystem namespace for my replacements, which would break compatibility.

Is there a mechanism by which I can cleanly replace System classes without doing one of those things?

Edit: One thought is to use Mono.Cecil to rewrite references to System.Console to Renraku.System.Console or somesuch, but I'd prefer to work within the framework if possible.

A: 

Are you looking just to redirect standard in and standard out? If so, Console.SetIn(TextReader in) and Console.SetOut(TextWriter out) are exactly what you're looking for.

As far as other methods go, I'm pretty sure they'll all go through mscorlib at some point. If can't afford that, you may end up writing your own Console class after all.

Juliet
We need lower level access to the console, e.g. colors, buffer size, etc. Thus we need to be able to replace the whole class. It's looking like the only way to go about this is to use Mono.Cecil to rewrite references.
Cody Brocious
A: 

I dont think with your restrictions to third party you can switch from system.console. why not write a new class with what you wanted or search for an open source stuff.

There is one more alternative is to use windows powershell, but i think that will also not work in your case

Apurva Saxena
A: 

how about indirecting on the console methods. Use reflection to load up function pointers to the functions you need and then you can have a config file that provides the actual names of the functions to use

Ie in your code you call DoConsoleRead

At load time DoConsoleRead is made to point to some arbitrary function

pm100
A: 

I think it could be done using the References node in the Properties sheet. Take a look at this example: http://www.codeproject.com/KB/mobile/Wm2005Globalization2.aspx. It "fixes" a class in the Compact Framework, I think you can make it work in the full framework, but I can test it right now. (No VS at work)

Pablo Grisafi
A: 

How about running ROTOR (SSCLI) on your OS? There you also have the full source code and an abstraction from the underlying OS http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en

Having spent some time in Reflector with MS .NET, we all know it is tightly bound to the WIN 32 API, which would only cause your OS to look more like Windows then be a new clean OS.

Other then that, rewriting references seems like a viable option.

Rudi
ROTOR, being under a tainting license, would be a serious legal issue if Renraku developers were to touch it; it'd block us from being able to work on our own runtime, and make our OS effectively non-free. Due to the architecture of the OS, platform-specific (hosted) features are isolated to different services, allowing us to maintain our own style. It looks like reference rewriting is the best way to go about it.
Cody Brocious
Cody, I've been looking into more Managed OS'es. And I now totally see your point. It seems like similar minded people from COSMOS and MOSA are having the same issues.Seems to me you should all agree to work on 1 single 'Managed OS' if you care to be successfull. There is just too much work to spread it across (at least 3 projects).
Rudi
A: 

What about replacing the System class in the GAC? I know Java has a bootclasspath switch define the location of the Java "bootstrap" classes-java.lang.Object, java.lang.Exception, and so forth

Eli Perpinyal
+3  A: 

You should look into the free tool PostSharp. It allows you to change existing .Net assemblies at design time by automating the modification of the MSIL.

See this page for more information on compile-time weaving in PostSharp. and this page for information on load-time weaving (occurs at runtime just before the assembly is loaded in memory).

For Runtime weaving (modifying existing methods at runtime), look at LinFu.

[Except from page]

... you can use LinFu.AOP to dynamically intercept any method on any type, regardless of whether or not the method is declared virtual or non-virtual. You’ll also be able to dynamically intercept (and even replace) methods declared on sealed types, in addition to any static methods that are declared on that type.

[/Except from page]

Gabriel McAdams
Thanks for the info. Not perfect for my case, but it seems to be the best I'll do.
Cody Brocious
Not a problem. I hope it does the trick.
Gabriel McAdams