Learning Mono Source Code [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am interested in contributing something to mono whether it is a documentation or what ever. As a first step, I downloaded the source tree for going through the code. However, I thought if some one would've spend enough time to understand the project structure that would help everyone here. Any one point me out where the project structure is well explained?
NOTE: This is not a duplicate of question https://stackoverflow.com/questions/1655090/mono-source-code-walkthrough-tutorial, the answer to this question doesn't suffice my expectation.

You should have checked out (subversion checkout URLs here):
trunk/libgdiplus
This is a library used by System.Drawing.
trunk/mono
This is what we call the Mono runtime. Contains mainly C source code. Under this directory you can find:
data/: a few configuration files for different version (1.x, 2.x,...).
msvc*/: Visual Studio solution files to build the Mono runtime.
libgc/: the Boehm Garbage Collector sources.
mono/: Mono runtime sources.
mini/: JIT source code
metadata/: these are almost all the functions used by the Mono runtime (marshaling, thread pool, socket I/O, file I/O, console I/O, application domains, GC, performance counters,...). It's more or less one C file each.
util: miscellaneous functions.
io-layer/: Win32 I/O emulation functions.
trunk/mcs
This is where the C# compiler, the class libraries, class libraries tests and other tools are.
class/ : One folder per assembly. Each of them contains the source code for each assembly split in directories with the namespace name (ie, System/System.Configuration and so on) and usually a Test directory too. The only naming exception is mscorlib whose corresponding folder is called corlib.
For example, if you want to see the source code for System.Net.HttpWebRequest, which is in the System.dll assembly, you go to trunk/mcs/class/System/System.Net and there shoould be a file named HttpWebRequest.cs containing the code you're looking for.
mcs/: the sources for the C# compilers (mcs, gmcs, smcs, dmcs...)
tools/: these are a bunch of tools used for development (sn, wsdl,...), documentation (monodoc), etc. Most of the tools names match the MS ones.
There are a lot more directories around, but those are where you should look for the C and C# code. Also, I suggested trunk for the checkout, since you will get the most up-to-date sources that way.
Update: Mono resides now in github and mcs has been integrated into the mono repository.

Gonzalo provided a good overview of the different modules.
Since you also mentioned wanting to contribute to documentation, you'll want a few more pieces of information.
First, Documentation is stored in XML files within mcs/class/[assembly]/Documentation/, e.g. mcs/class/corlib/Documentation. The intent is to support multiple human languages (though only English is currently being worked on), so within Documentation is a language directory, usually en. Within en there are ns-*.xml files, e.g. mcs/class/corlib/Documentation/en/ns-System.xml contains documentation for the System namespace. Also within en are "dotted namespace" directories, and within those are XML files, one per type, for example mcs/class/corlib/Documentation/en/System.Collections.Generic/IEnumerable`1.xml.
This is also outlined within the mdoc(5) documentation, in the FILE/DIRECTORY STRUCTURE section.
Once you've found the documentation, you need to know the XML format, which is also described in the mdoc(5) documentation, in the NamespaceName/TypeName.xml File Format section. The XML dialect used is a variant of the ECMA 335 XML documentation, changed to have one file per type (instead of all types within a single monolithic file). This is also a superset of C# XML documentation (see Annex E. Documentation Comments, page 487).
Finally, there's the question of adding new types/members to the mcs/class/[assembly]/Documentation directory. If you have Mono built, you can use the doc-update Makefile target. This will run the appropriate assembly through mdoc(1) and update the appropriate files within the Documentation directory.
If you have any other documentation questions, don't hesitate to ask on the mono-docs-list mailing list.

Related

How to make a C# library translatable? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I've written a (open source) C#/.NET library that contains a handful of strings that may be displayed to the user. Thus, it would be good to have them translatable.
I've worked at a couple of companies now and they always solved this problem via .resx files. However, as companies, they a) know exactly which languages their applications will be translated to and b) have the resources (man power, money) to have all of their strings translated.
As an open source author I neither want to limit the translation of my library to a certain set of languages nor do I have the resources to provide any translation at all.
So, ideally I would only provide the English "translation" for all my strings and user's of my library would have some way of translating these strings into their desired languages without any code changes to my library.
To my (limited) understanding, when using .resx files the default language (English) is compiled directly into the assembly/dll whereas other languages are provided as satellite assemblies. So, in theory, user's of my library could provide the satellite assemblies for their desired languages themselves.
Would this work for open source libraries (and if yes, how)? Or are there other, better (recommended) ways of how to deal with this problem?
(Ideally the solution should work with .NET Core.)
Having users of your library provide translations is not uncommon or unreasonable, I guess. At work we do the same with a commercial library where we also don't have the resources to provide all languages out of the box.
Translation still works with satellite assemblies, the only complicated part is to get the resource names correct (they use the default namespace of the project + any folders if you don't provide a custom name in the project file) so that they are picked up correctly at runtime.
You could use JSON to solve your translation problem
Slay the Spire is a really fun rouge-like deck building game
and to translate the game to various languages they came to the community with guidelines and files (which are basically JSON files).
Of course, i don't know the ins and outs of how they did that exactly but it seems you can use the same thing for your library
you can check the computer local language (or any other way) to get the user main language and pull(if exists) the right JSON file before the program starts up
TRANSLATOR_README
example for french translation

identify classes referencing unnecessary interfaces [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have to refactor a large C# application, and I found a lot of functions that are never used. How can I check for unused code, so I can remove all the unused functions?
Yes, ReSharper does this. Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.
It's a great question, but be warned that you're treading in dangerous waters here. When you're deleting code you will have to make sure you're compiling and testing often.
One great tool come to mind:
NDepend - this tool is just amazing. It takes a little while to grok, and after the first 10 minutes I think most developers just say "Screw it!" and delete the app. Once you get a good feel for NDepend, it gives you amazing insight to how your apps are coupled. Check it out: http://www.ndepend.com/. Most importantly, this tool will allow you to view methods which do not have any direct callers. It will also show you the inverse, a complete call tree for any method in the assembly (or even between assemblies).
Whatever tool you choose, it's not a task to take lightly. Especially if you're dealing with public methods on library type assemblies, as you may never know when an app is referencing them.
Resharper is good for this like others have stated. Be careful though, these tools don't find you code that is used by reflection, e.g. cannot know if some code is NOT used by reflection.
As pointed Jeff the tool NDepend can help to find unused methods, fields and types.
To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection
Basically such a rule to detect unused method for example looks like:
// <Name>Dead Methods</Name>
warnif count > 0
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m
But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:
Potentially dead Types (hence detect unused class, struct, interface, delegate...)
Potentially dead Methods
Potentially dead Fields
NDepend integrates in Visual Studio 2022, 2019, 2017,2015, 2013, 2012, 2010, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements. NDepend has also a VS Team Services extension.
If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).
This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.
In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.
Disclaimer: I work for NDepend.
I would also mention that using IOC aka Unity may make these assessments misleading. I may have erred but several very important classes that are instantiated via Unity appear to have no instantiation as far as ReSharper can tell. If I followed the ReSharper recommendations I would get hosed!
ReSharper does a great job of finding unused code.
In the VS IDE, you can right click on the definition and choose 'Find All
References', although this only works at the solution level.
The truth is that the tool can never give you a 100% certain answer, but coverage tool can give you a pretty good run for the money.
If you count with comprehensive unit test suite, than you can use test coverage tool to see exactly what lines of code were not executed during the test run. You will still need to analyze the code manually: either eliminate what you consider dead code or write test to improve test coverage.
One such tool is NCover, with open source precursor on Sourceforge. Another alternative is PartCover.
Check out this answer on stackoverflow.
I have come across AXTools CODESMART..Try that once.
Use code analyzer in reviews section.It will list dead local and global functions along with
other issues.
FXCop is a code analyzer... It does much more than find unused code. I used FXCop for a while, and was so lost in its recommendations that I uninstalled it.
I think NDepend looks like a more likely candidate.

Looking for a C# code parser [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm looking for a set of classes (preferably in the .net framework) that will parse C# code and return a list of functions with parameters, classes with their methods, properties etc. Ideally it would provide all that's needed to build my own intellisense.
I have a feeling something like this should be in the .net framework, given all the reflection stuff they offer, but if not then an open source alternative is good enough.
What I'm trying to build is basically something like Snippet Compiler, but with a twist. I'm trying to figure out how to get the code dom first.
I tried googling for this but I'm not sure what the correct term for this is so I came up empty.
Edit: Since I'm looking to use this for intellisense-like processing, actually compiling the code won't work since it will most likely be incomplete. Sorry I should have mentioned that first.
While .NET's CodeDom namespace provides the basic API for code language parsers, they are not implemented. Visual Studio does this through its own language services. These are not available in the redistributable framework.
You could either...
Compile the code then use reflection on the resulting assembly
Look at something like the Mono C# compiler which creates these syntax trees. It won't be a high-level API like CodeDom but maybe you can work with it.
There may be something on CodePlex or a similar site.
UPDATE
See this related post. Parser for C#
If you need it to work on incomplete code, or code with errors in it, then I believe you're pretty much on your own (that is, you won't be able to use the CSharpCodeCompiler class or anything like that).
There's tools like ReSharper which does its own parsing, but that's prorietary. You might be able to start with the Mono compiler, but in my experience, writing a parser that works on incomplete code is a whole different ballgame to writing one that's just supposed to spit out errors on incomplete code.
If you just need the names of classes and methods (metadata, basically) then you might be able to do the parsing "by hand", but I guess it depends on how accurate you need the results to be.
Mono project GMCS compiler contains a pretty reusable parser for C#4.0. And, it is relatively easy to write your own parser which will suite your specific needs. For example, you can reuse this: http://antlrcsharp.codeplex.com/
Have a look at CSharpCodeCompiler in Microsoft.CSharp namespace. You can compile using CSharpCodeCompiler and access the result assembly using CompilerResults.CompiledAssembly. Off that assembly you will be able to get the types and off the type you can get all property and method information using reflection.
The performance will be pretty average as you will need to compile all the source code whenever something changes. I am not aware of any methods that will let you incrementatlly compile snippets of code.
Have you tried using the Microsoft.CSharp.CSharpCodeProvider class? This is a full C# code provider that supports CodeDom. You would simply need to call .Parse() on a text stream, and you get a CodeCompileUnit back.
var codeStream = new StringReader(code);
var codeProvider = new CSharpCodeProvider();
var compileUnit = codeProvider.Parse(codeStream);
// compileUnit contains your code dom
Well, seeing as the above does not work (I just tested it), the following article might be of interest. I bookmarked it a good long time ago, so I believe it only supports C# 2.0, but it might still be worth it:
Generate Code-DOMs directly from C# or VB.NET
It might be a bit late for Blindy, but I recently released a C# parser that would be perfect for this sort of thing, as it's designed to handle code fragments and retains comments:
C# Parser and CodeDOM
It handles C# 4.0 and also the new 'async' feature. It's commercial, but is a small fraction of the cost of other commercial compilers.
I really think few people realize just how difficult parsing C# has become, especially if you need to resolve symbolic references properly (which is usually required, unless maybe you're just doing formatting). Just try to read and fully understand the Type Inference section of the 500+ page language specification. Then, meditate on the fact that the spec is not actually fully correct (as mentioned by Eric Lippert himself).

Comparison of XSD Code Generators [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm doing some research in code generation from xsd schema files.
My requirements:
Must generate C# 2.0 code (or above), using generic collections where needed.
Must generate comments from the xsd comments
Must generate fully serializable code.
Should be able to generate resuable basetypes when generating from multiple xsd's with the same includes.
(see also my other questions: How can I generate multiple classes from xsd’s with common includes? and How can I generate comments from xs:documentation tags in a wsdl?
I have found the following options:
Use xsd.exe (supplied with the SDK and Visual Studio)
XSDCodeGen from Daniel Cazzulino
Xsd2Code
CodeXS
XsdObjectGen by Microsoft
XSDClassGen (Seems to be missing in action)
Did I miss any? Because (1), (2) and (5) do not generate 2.0 code, and I have problems with serializing code from (3). What do you use when generating code?
I believe XSD2Code is the best tool currently available (in 2011).
I recently went through the same process at work of analysing the available tools out there so i thought i would provide an updated answer that relates to VS2010.
Our main driver was that xsd.exe does not generate XML doc from the XSD annotations, which we wanted as we have hundreds of type definitions. I tried all the tools listed above as well as others and most were either deprecated, unmaintained or unable to match the current functionality of xsd.exe available in VS2010.
Xsd2Code however is a superb tool and seems to be actively maintained. It provides all the functionality that was listed above and a lot more - the CodePlex page also has great examples of how the various options affect output.
It also has tight VS integration, including context menu integration and a custom build tool (which means that if you reference the XSDs in your project and specify the custom tool, it will automatically update the code as you update the XSD). All in all saved us a lot of work.
A quick summary of the other tools i looked at:
Dingo - Seems to be more aligned to Java
XSDCodeGen - More of a demo on how to write a custom build tool
CodeXS - Quite a good tool, but less integration, features and no longer maintained
XSDObjectGen - No longer maintained, less functionality than current xsd.exe
XSDClassGen - Could not locate it
OXM Library - Recommend looking at this project, maintained and great functionality
LINQ to XSD - Very cool project, but not what i was looking for
Addendum:
If you do decided to go ahead with XSD2Code, there are a number of issues i found working with the command-line tool. In particular, there are some bugs with the argument processing that require some arguments to be in a certain order as well as some undocumented dependencies (eg - automatic parameters & .NET version are order specific and dependent). The following are the steps i used to generate the code using XSD2Code and then cleanup the output - take the bits that apply to you as necessary:
Run the following batch file to generate the initial code, changing the paths to the correct locations:
#echo off
set XsdPath=C:\schemas
set OutPath=%XsdPath%\Code
set ExePath=C:\Progra~1\Xsd2Code
set Namespace=InsertNamespaceHere
echo.Starting processing XSD files ...
for /f %%a IN ('dir %XsdPath%\*.xsd /a-d /b /s') do call:ProcessXsd %%a
echo.Finished processing XSD files ...
echo.&pause&
goto:eof
:ProcessXsd
%ExePath%\Xsd2Code %~1 %Namespace% %XsdPath%\Code\%~n1%.cs /pl Net35 /if- /dc /sc /eit
echo.Processed %~n1
goto:eof
Perform the following steps to tidy up the generated code, as necessary:
Regex replace - current project, case, whole word - [System.Runtime.Serialization.DataContractAttribute(Name:b=:b:q,:bNamespace:b=:b*{:q})] with [DataContract(Namespace = \1)]**
Replace - current project, case, whole word - [System.Runtime.Serialization.DataMemberAttribute()] with [DataMember]
Regex replace - current project, case, whole word - System.Nullable<{:w}> with \1?
Regex replace - open documents, case, whole word - {:w}TYPE with \1
Replace - open documents, case, whole word - System.DateTime with DateTime, then add missing using statements
Replace - open documents, case, whole word - [System.Xml.Serialization.XmlIgnoreAttribute()] with [XmlIgnore]
Replace - current project - System.Xml.Serialization.XmlArrayAttribute with XmlArray
Replace - current project - System.Xml.Serialization.XmlArrayItemAttribute with XmlArrayItem
Regex replace - current project - ,[:Wh]+/// <remarks/> with ,
I have not yet checked this out, but Linq2XSD might be a useful alternative.
I'm going to give this one a shot. LINQ with XSD generation would be better than any of these tools you mentioned - provided it works nicely.
OpenSource project XSD to Classes worked perfect for me.
I a project a bit over a year ago we used CodeXS. With some minor adjustments (a script that cleaned up the generated code a bit) it worked a charm.
There is also Dingo, which have some very good extensibility features (which we didn't need).
Try OxmLibrary - http://oxmlibrary.codeplex.com
The best XSD class generator I've found is
thinktecture WSCF.blue
.
It's nicer than most of the others for two reasons:
Fixes naming. That means casing and plurization of types and property names.
Creates a separate file for each class.
Or, if you're looking for a T4 solution, you can try
XsdClassGen
.
This one isn't working for me. But the good news is that it's a T4 file, so it's easy to fix!
Here is web based example of using XSLT to transform XML to C# code.
The example takes a model (XML) that describes services (basically service names, namespaces, and list of operations), it then generates WCF services (interfaces, messages, faults, tests, etc) all in C#.net.

Find unused code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have to refactor a large C# application, and I found a lot of functions that are never used. How can I check for unused code, so I can remove all the unused functions?
Yes, ReSharper does this. Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.
It's a great question, but be warned that you're treading in dangerous waters here. When you're deleting code you will have to make sure you're compiling and testing often.
One great tool come to mind:
NDepend - this tool is just amazing. It takes a little while to grok, and after the first 10 minutes I think most developers just say "Screw it!" and delete the app. Once you get a good feel for NDepend, it gives you amazing insight to how your apps are coupled. Check it out: http://www.ndepend.com/. Most importantly, this tool will allow you to view methods which do not have any direct callers. It will also show you the inverse, a complete call tree for any method in the assembly (or even between assemblies).
Whatever tool you choose, it's not a task to take lightly. Especially if you're dealing with public methods on library type assemblies, as you may never know when an app is referencing them.
Resharper is good for this like others have stated. Be careful though, these tools don't find you code that is used by reflection, e.g. cannot know if some code is NOT used by reflection.
As pointed Jeff the tool NDepend can help to find unused methods, fields and types.
To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection
Basically such a rule to detect unused method for example looks like:
// <Name>Dead Methods</Name>
warnif count > 0
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m
But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:
Potentially dead Types (hence detect unused class, struct, interface, delegate...)
Potentially dead Methods
Potentially dead Fields
NDepend integrates in Visual Studio 2022, 2019, 2017,2015, 2013, 2012, 2010, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements. NDepend has also a VS Team Services extension.
If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).
This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.
In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.
Disclaimer: I work for NDepend.
I would also mention that using IOC aka Unity may make these assessments misleading. I may have erred but several very important classes that are instantiated via Unity appear to have no instantiation as far as ReSharper can tell. If I followed the ReSharper recommendations I would get hosed!
ReSharper does a great job of finding unused code.
In the VS IDE, you can right click on the definition and choose 'Find All
References', although this only works at the solution level.
The truth is that the tool can never give you a 100% certain answer, but coverage tool can give you a pretty good run for the money.
If you count with comprehensive unit test suite, than you can use test coverage tool to see exactly what lines of code were not executed during the test run. You will still need to analyze the code manually: either eliminate what you consider dead code or write test to improve test coverage.
One such tool is NCover, with open source precursor on Sourceforge. Another alternative is PartCover.
Check out this answer on stackoverflow.
I have come across AXTools CODESMART..Try that once.
Use code analyzer in reviews section.It will list dead local and global functions along with
other issues.
FXCop is a code analyzer... It does much more than find unused code. I used FXCop for a while, and was so lost in its recommendations that I uninstalled it.
I think NDepend looks like a more likely candidate.

Categories

Resources