简体   繁体   中英

Programatically renaming or excluding C# classes and methods

I'm writing an HTML parser using SharpKit , which outputs to Javascript; but would also like to run the same code on occasion in CsQuery , which outputs a binary.

The code that I have for SharpKit almost can be directly used in CsQuery, but not quite, as:

  • The namespaces have to be changed, and function/class names changed accordingly.
  • I also have a couple of classes that emulate .NET classes in Sharpkit that I don't need in the CsQuery code at all.

I'd like to be able to do is keep my code synchronized by performing these changes programatically. Is this possible? The solutions here are too old to work with C# 4.0, but I feel like Rosalyn should be able to do this.

You can rename methods in SharpKit using the JsMethodAttribute:

[JsMethod(Name="doSomething")]
public void DoSomething(){}

You can exclude method the same way by setting Export=false:

[JsMethod(Export=false)]
public void DoSomething(){}

You should be able to use the exact same code if you use Clr mode (requires jsclr.js library). Or by using native Js and .NET methods, while extending any missing method in JavaScript, for example, if JavaScript doesn't have a String.StartsWith method, you can either route it to a different method, or implement it yourself using extension methods.

For more extensive help and code samples, try the SharpKit forum: https://groups.google.com/forum/#!forum/sharpkit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM