开发者

Replacing Classes in the System Namespace

I'm currently working to hack my .NET operating system into开发者_JS百科 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.


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]


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.


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


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


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)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜