Generating doxygen comments for swig-generated C# that wraps C++ - c#

I have a project written in C++ where I'm using swig to generate some C# wrappers as well. The C++ code uses Doxygen style comments to annotate the classes and functions. Is it possible to get Swig to take those doxygen comments and produce doxygen comments for the C# wrapper classes and functions?

Currently, SWIG does not parse code comments including Doxygen documentation at all.
There is a SWIG branch in development since a couple of years to enable SWIG to deal with Doxygen comments, but even that currently (AFAIK) only maps them to Java and Python documentation.
The best option currently is therefore to extract the Doxygen documentation from the C++ source code and insert it into the SWIG generated wrapper. To understand how this can be done, here is a brief explanation of what doxy2swig.py does (and this is indeed meant for python docstrings):
Let Doxygen extract the documentation into its XML format
Parse the XML, and reformat into suitable Python docstrings
Write %feature("docstring") SWIG directives to tell SWIG to attach the docstrings to the wrapped classes and methods.
Basically, something similar can be done for C# as well. I do not know how to do (2) for C#, i.e., how to translate the Doxygen XML output into suitable C# documentation, this you may need to implement yourself (perhaps by modifying the doxy2swig.py script).
For (3) there is a neat trick that is sort of documented here, noting that the same can also be done for C# using the %csclassmodifiers and %csmethodmodifiers. These SWIG feature directives are AFAIK used to prepend either public or protected to C# methods or classes. But they can be hijacked to prepend the extracted documentation (+ the public keyword, not to forget). So they effectively allow the same functionality as the %feature("docstring") directive for Python.
Finally, I don't know C#, but what is the point of having the Doxygen comments included in the C# wrapper? If you only want to use Doxygen to generate documentation, you can do this from the C++ sources directly, so you don't gain anything. In Python, the docstrings can be displayed as help at runtime, and are used by some IDEs. Does C# have this, too?

As of October 2022, the accepted answer from m7thon has become outdated. I have started work in the (public) merge request https://github.com/swig/swig/pull/2421, based on the nice prior work from https://github.com/swig/swig/pull/1695, to add support for doxygen comments for SWIG-generated C#.
The current status in the above MR still has quite significant limitations. Also it has not yet been extensively tested. But it can already achieve basic documentation in C# XML format, and may be a good starting point for people in need of a solution.

Related

What is Deconstructed Compiler ? How C# gains a dynamic language's ability to generate and invoke code at runtime via Roslyn?

After reading this article about Roslyn. I came across two things that i did not understand.
deconstructed compiler
C# gains a dynamic language's ability to generate and invoke code at runtime via Roslyn
I have searched a lots of posts on stack overflow and googled for it. but could not get the whole picture.
Can anyone please explain to me or direct me with some links and resources about these topics.
Taken from the linked article:
Hejlsberg demonstrated a C# program that passed a few code snippets to
the C# compiler as strings; the compiler returned the resulting IL
assembly code as an object, which was then passed to the Common
Language Runtime (CLR) for execution. VoilĂ ! With Roslyn, C# gains a
dynamic language's ability to generate and invoke code at runtime.
The part of:
[...] C# gains a dynamic language's ability to generate and invoke code at runtime.
...is just a very wrong assumption made by the blog post author...
Compiling code from an application doesn't turn C# into a dynamic language or it doesn't turn new C# compiler in a substitute of an interpreter...
C# was able to generate code at run-time since its inception using Reflection Emit. It seems like the new compiler didn't add that feature, but anyway it's easier to generate code from regular C# code with the new compiler than using Reflection Emit. In addition, as #hvd has noted in some comment, it was also possible since C# inception using CSharpCodeProvider.
C#, since .NET 4.0, can interoperate with dynamic languages using the Dynamic Language Runtime, which was created to open the door to interpreted language implementations on top of .NET (and also to make COM interop easier...).
About the other question (the thing about deconstructed compiler), it means that the new C# compiler allows you to hook other code to perform actions based on C# compilation steps.
I would take a look at Roslyn Overview on GitHub where there're a lot of details that should give more depth on the topic.

Is it possible to add keyword to C# or VB.NET?

I know it might not be worth it but just for education purposes I want to know if there is a way to inject your own keywords to .NET languages.
For example I thought it's good to have C++ asm keyword in C#.
Remember I'm not talking about how to implement asm keyword but a general way to add keyword to C#.
My imagined code :
asm{
mov ax,1
add ax,4
}
So is there a way to achieve this ?
The answers which cover implementing keyword{ } suits enough for this question.
This isn't possible at the moment. However, there's a Microsoft project in development called Roslyn that can be summarised as "the compiler as a service." It allows you, amongst other things, to extend or modify the behaviour of the compiler through an API.
When Roslyn becomes available, I believe this should be something that (with caution!) is quite doable.
You can use whatever tools you would like to pre-process your code before sending it to the C# compiler. For example, you might use VS macros to do the pre-processing, mapping a given syntax that you invented into something that does compile into C# code, possibly generating an error if there is a problem. If VS macros aren't powerful enough for you then you can always use your own IDE that does whatever you code it to do to the text before sending it to the compiler.
There is no built in support in the compiler for specifying your own keywords/syntax; you would need to handle it entirely independent of the compiler.
Unfortunately this is not possible. You can't extend or alter the languages in any way.
You could in some obscure way use PostSharp to read and parse strings and transform them to custom code at compile time (a pre processor). But you would not get very happy with that, as it is very error prone and you won't get any kind of intellisense or code completion for your magic strings.
According to MSDN keywords are predefined and cannot be altered. So you can't add any, because you would need to tell the compiler how to handle them. Insofar, no you can't.

SandCastle: Generating links for C# class name

When I used doxygen for my C++ projects, I simply wrote:
/**
* I can refer to SomeClass or even SomeClass::someMethod() without special markup.
*/
Then doxygen generates links from "SomeClass" and "SomeClass::someMethod()" strings to appropriate documentation.
Now I work with C# and want to use SandCastle for generation documentation form C# code. I realized that links should be constructed with xml tag:
/// <see cref="SomeClass"/> and <see cref="SomeClass.someMethod()"/>
I think that it is very cumbersome and unreadable.
Is it any way to generate links for class and methods without special markup?
How do you generate documentation for C# projects?
Thank you.
Is it any way to generate links for class and methods without special
markup?
I don't think so. Actually the XML documentation (format) is not a feature of sandcastle, but of the C# compiler. Sandcastle (and other tools, like NDoc) "simply" build on it.
How do you generate documentation for C# projects?
We're using Sandcastle and the Sandcastle Helpfile Builder (SHFB).
Also we're using GhostDoc and ReSharper which help with writing and (on-the-fly) validating XML comments, for example if the type, method, or else you reference actually exist.
I think you could also use Doxygen and thus Doxygen style comments with your C# code, but personally I have never tried it. You'd have to check the Doxygen website. But you would miss out on features other tools provide based on the XML documentation (like ReSharper's quick documentation) or most prominently, help-/description texts for Intellisense.

Can we decompile a C# dll to VB.NET

I have developed a dll in C#.net. Can we decompile the same in VB.net. If yes, how to do that?
Use a tool like Reflector to convert a compiled C# assembly (IL) into decompiled VB.NET source code (might lose some meaning with certain variable names). Another post mentions some free alternatives to Reflector.
Or convert the C# code using an online resource such as this one (1 code file at a time though).
I agree with Mitch, but as a free alternative to Reflector:
ILSpy
dotPeek
Assuming you have the source, you can use sharpdevelop to convert whole solutions back and forth between c#.net and vb.net
The same group also developed a plugin for visual studio: Code Converter C# to/from VB.NET
Sometimes C# cannot be converted (or decompiled) into VB.Net because there are some elements of C# that have no equivalent in VB. E.g. iterators (yield) and unsafe code. Furthermore some of the free converters fail badly even when there are direct equivalents.
Why do you want to do this anyway? It's probably easy to just use the C# DLL from VB.Net code: you can call it, you can inherit from it. "CLS compliance" will help with this.
For what it's worth, it's also true that there are some elements of VB that have no equivalent in C#, e.g. exception filters.
If you've developed it your self I assume you have access to the source? If so you can just convert it all though here:
http://www.developerfusion.com/tools/convert/csharp-to-vb/

Code parsing C#

I am researching ways, tools and techniques to parse code files in order to support syntax highlighting and intellisence in an editor written in c#.
Does anyone have any ideas/patterns & practices/tools/techiques for that.
EDIT: A nice source of info for anyone interested:
Parsing beyond Context-free grammars
ISBN 978-3-642-14845-3
My favourite parser for C# is Irony: http://irony.codeplex.com/ - i have used it a couple of times with great success
Here is a wikipedia page listing many more: http://en.wikipedia.org/wiki/Compiler-compiler
There are two basic aproaches:
1) Parse the entire solution and everything it references so you understand all the types involved in the code
2) Parse locally and do your best to guess what types etc are.
The trouble with (2) is that you have to guess, and in some circumstances you just can't tell from a code snippet exactly what everything is. But if you're happy with the sort oif syntax highlighting shown on (e.g.) Stack Overflow, then this approach is easy and quite effective.
To do (1) then you need to do one of (in decreasing order of difficulty):
Parse all the source code. Not possible if you reference 3rd party assemblies.
Use reflection on the compiled code to garner type information you can use when parsing the source.
Use the host IDE's (if avaiable - so not applicable in your case!) code element interfaces to provide the information you need
You could take a look at how http://www.icsharpcode.net/ did it. They wrote a book doing just that, Dissecting a C# Application: Inside SharpDevelop, it even has a chapter called
Implement a parser to provide syntax
highlighting and auto-completion as
users type

Categories