6.4.2. Source Code Analysis for C#/.NET¶
6.4.2.1. Introduction¶
The information in this chapter is only relevant to the C#/.NET variant of the Axivion Suite.
For the C# analysis, the first step is to ensure that the project(s) to be analyzed
can be built from the command line in the environment executing the analysis (using msbuild,
dotnet msbuild, or dotnet build). This is necessary because csharp2rfg
interprets solutions and project files very similarly to msbuild, but instead of using
csc to produce binary outputs, it uses all source code files, binary references, and
configuration options collected by msbuild to generate an RFG and calculate metrics,
or to produce a C# package that can be loaded and analyzed later.
If you are using a different build system (such as cake), it may be necessary to first
generate MSBuild solution/project files for use with csharp2rfg.
The following tools can be used with C#:
- axivion_ci
Using the AxivionC#Frontend build step, a C# program – consisting of one or more C# MSBuild projects – can be compiled, and various Axivion analysis rules can be configured to analyze the C# source code.
- csharp2rfg
csharp2rfgcompiles C# source code into a C# package, which can be loaded and analyzed later, or it creates an RFG and calculates metrics.- axivion_analysis
axivion_analysiscan apply Axivion analysis rules to a pre-built C# package (.cspkg) created by the AxivionC#Frontend build step (invoked byaxivion_ci) orcsharp2rfg.
Note
This chapter focuses on the description of csharp2rfg. The AxivionC#Frontend
build step uses csharp2rfg internally, so the description also applies to it,
unless noted otherwise.
6.4.2.2. Requirements¶
The current version of csharp2rfg requires .NET 10.0 to run and can be executed
on any platform that supports .NET 10.0 (recommended runtime version: 10.0.12 or higher).
csharp2rfg is capable of processing any valid C# program as accepted by
Microsoft’s C# compiler csc.exe. The currently supported versions of C# are:
C# 1.0 (Visual Studio .NET)
C# 2.0 (Visual Studio 2005)
C# 3.0 (Visual Studio 2008)
C# 4.0 (Visual Studio 2010)
C# 5.0 (Visual Studio 2012)
C# 6.0 (Visual Studio 2015)
C# 7.x (Visual Studio 2017)
C# 8.0 (Visual Studio 2019)
C# 9.0 (Visual Studio 2019 [16.8] / .NET 5.0)
C# 10.0 (Visual Studio 2022 [17.0] / .NET 6.0)
C# 11.0 (Visual Studio 2022 [17.4] / .NET 7.0)
C# 12.0 (Visual Studio 2022 [17.8] / .NET 8.0)
C# 13.0 (Visual Studio 2022 [17.12] / .NET 9.0)
C# 14.0 (Visual Studio 2026 [18.0] / .NET 10.0)
Using csharp2rfg with a newer version of C# is possible, but the analysis results
are likely incomplete and may produce errors; therefore, this is generally not recommended.
csharp2rfg supports processing MSBuild solutions, solution filters, and project
files using the following MSBuild versions:
MSBuild 15.3 or higher (Visual Studio 2017)
MSBuild 16.x (Visual Studio 2019)
MSBuild 17.x (Visual Studio 2022)
MSBuild 18.x (Visual Studio 2026)
Using csharp2rfg with a newer or older version of MSBuild is theoretically possible,
but may
require additional configuration and is generally not recommended.
6.4.2.3. Usage with axivion_ci¶
Note
This section describes how to manually configure axivion_ci by using axivion_config. The setup wizard currently does not support C#.
csharp2rfg should be invoked as part of axivion_ci by using the AxivionC#Frontend action in BuildSystemIntegration.
It can be configured by following these steps:
Prerequisites¶
Before configuring AxivionC#Frontend for the first time in a project, it is necessary to set some global properties:
Set /Project/directory to the repository root.
Set /Project/name to the name of the project.
Set /Results/Dashboard/dashboard_url to the URL where the Axivion Dashboard is hosted.
If /Results/Dashboard/database_mode is
shared_database, set the environment variableAXIVION_DATABASES_DIRto the correct location.
Setup of AxivionC#Frontend¶
Create a new copy of AxivionC#Frontend.
Set the field
inputto the item you want to analyze. This can be one of the following:
a single
C# source code file (.cs)a directory containing one or more
C# source code files (.cs)a single Visual Studio
C# project file (.csproj)a single
Visual Studio Solution file (.sln, .slnx)a single
Visual Studio Solution filter file (.slnf)
3. If you are analyzing a single C# source code file (.cs) or a directory without
any MSBuild project files:
a. Set the field build_target_framework to the target framework name, for example
net8.0. This field is mandatory for this kind of input.b. If it does not contain a
Mainmethod, it may be necessary to set the fieldproject_is_libraryaccordingly.c. Set the field
framework_pathto the directory containing all assemblies (including BCL [base-class library] assemblies) of that target framework. If the field is not set, the framework assemblies of the .NET runtime that runscsharp2rfgare used.
4. If you are analyzing a project or solution, csharp2rfg tries to detect the
matching MSBuild version automatically. If that fails, you must ensure that
the correct version of MSBuild is used; you can manually set the msbuild_path
option to the correct version.
5. Ensure that the correct configuration and platform are used. Unlike Visual Studio,
csharp2rfg might not be able to infer them correctly; therefore, it is advisable
to set them explicitly using the fields build_configuration and build_platform.
6. If your project uses new-style (aka SDK-style) projects and you target multiple
frameworks, it may be necessary to choose one target framework for analysis. An analysis
cannot contain multiple target frameworks. You can use the field build_target_framework
to specify one explicitly.
7. If your project requires additional MSBuild variables, you can use the field properties
to pass a list of key-value pairs.
Note
If any problems occur during the execution of csharp2rfg or axivion_ci,
it is recommended to set the binarylog option and the --verbose flag to better
understand the issue.
Note
For cycle detection, it is necessary to disable the create_multiple_edges setting.
8. Configure analyses and additional build steps as necessary. Note that not every rule is applicable to C#. Please see Limitations for more information.
6.4.2.4. Usage with axivion_analysis¶
axivion_analysis is intended for use with pre-built C# package files (.cspkg)
and can be used to run analyses without first executing any build step.
axivion_analysis accepts a –csharp PACKAGE_FILE option.
A C# package file (.cspkg) can be created by using csharp2rfg on the command line
or by using the AxivionC#Frontend build step from axivion_ci.
About C# package files¶
Caution
C# package files contain potentially sensitive information and artifacts.
C# packages are archives that store all information necessary to compile a C# program. This includes all source code, resources, binary references, strong-name keys, and compiler configuration. Its contents will be visible to anyone having access to the C# package file.
6.4.2.5. Standalone usage¶
Standalone usage is similar to the steps used when setting up axivion_ci for C#.
Most options that can be set in the configuration UI can be directly passed to csharp2rfg
on the command-line as well. The fields build_in_parallel and cwd have no
command-line equivalent. The field create_multiple_edges is disabled by default, so
AxivionC#Frontend creates no duplicate edges, whereas csharp2rfg creates
them unless --no_duplicate_edges is given.
Command-Line Switches¶
csharp2rfg
[-h]
[--library]
[--no_duplicate_edges]
[--no_restore]
[--only_used_types]
[--bytecode_analysis]
[--configuration CONFIGURATION]
[--target_framework TARGET_FRAMEWORK]
[-t TARGETS]
[--platform PLATFORM]
[-p KEY=VALUE]
[-r REFERENCE]
[--out OUT]
[--framework_path FRAMEWORK_PATH]
[--binarylog [BINARYLOG]]
[-B BASEPATH]
[--msbuild_path MSBUILD_PATH]
[--list_msbuild_paths]
[--verbose]
input output
Creates an RFG from provided C# source file(s) or projects.
- input
Input path; can be a .cs, .csproj, .sln, .slnx, or .slnf file, or a directory containing .cs files.
- output
Output file; a path ending in .rfg creates an RFG, while any other path (e.g., ending in .cspkg) creates a C# package.
- --help
Show this help message and exit.
- --library
When analyzing a single file or directory, use this flag to indicate whether the result is a library (i.e., has no Main method/entry-point) or not.
- --no_duplicate_edges
The RFG should not contain multiple edges of the same kind (ignoring edge attributes) between two nodes. This occurs, for example, when a method calls the same method multiple times.
- --no_restore
Do not execute a package restore before compilation.
- --only_used_types
Only include types defined in source code and their dependencies in the RFG.
- --bytecode_analysis
Parse bytecode and include information from binary references in the RFG.
- --configuration CONFIGURATION
Solution configuration to use for compilation.
- --target_framework TARGET_FRAMEWORK
Solution target framework to use for compilation. Mandatory when analyzing a single file or directory; otherwise, it is detected automatically if left unspecified.
- --targets TARGETS
Build targets to invoke. Multiple values can be specified, separated by commas or semicolons. Default: Clean,Build (short form: -t)
- --platform PLATFORM
Solution platform to use for compilation.
- --property PROPERTY
Additional MSBuild properties to use for compilation. Can be used repeatedly to define multiple properties. (short form: -p)
- --reference REFERENCE
References used when analyzing a single file or directory. Can be used repeatedly to define multiple references. (short form: -r)
- --out OUT
When analyzing a single file or directory, specifies the name of the output assembly. This is used when resolving InternalsVisibleTo attributes.
- --framework_path FRAMEWORK_PATH
When analyzing a single file or directory that depends on .NET Core or later, use this option to set the path to framework assemblies. If absent, the framework assemblies of the .NET runtime that runs
csharp2rfgare used.
- –binarylog [BINARYLOG]
Produces a binary log of the build. A filename can be specified using
--binarylog <filename>. If no filename is set,msbuild.binlogis used. A filename that does not end in.binlogis interpreted as a directory name, and the binary log is stored asmsbuild.binlogin that directory. Relative filenames are resolved against the directory containing the input.
- --basepath BASEPATH
Specify the base path of your project. If absent, it is inferred from the environment or input path.
- --msbuild_path MSBUILD_PATH
Sets the path to the version of MSBuild to be used in the analysis; this should be set to the MSBuild version used to build the project being analyzed. This should point directly to MSBuild.exe (or MSBuild.dll for newer versions of .NET).
- --list_msbuild_paths
Prints a list of detected MSBuild installations and exits.
- --verbose
Enables extra debug output. (short form: -v)
6.4.2.6. Integration of third-party analyzers and checks¶
axivion_ci or csharp2rfg support the execution of third-party
Roslyn-based analyses. In general, analyzers can be consumed from a VSIX package or a
NuGet package. Axivion currently only supports executing analyzers from NuGet
packages using a PackageReference.
The Axivion rule C#-ImportCompilerAndAnalyzerDiagnostics can be used to import any compiler warnings and analyzer diagnostics into the Axivion dashboard.
If you want to skip the execution of third-party analyzers during the execution of
AxivionC#Frontend or csharp2rfg, you can set the MSBuild properties
RunAnalyzers or RunAnalyzersDuringBuild to false.
6.4.2.7. Limitations¶
csharp2rfg does not produce an IR and therefore does not support all rules and
checks provided by the Axivion Suite.
The following Axivion Suite core features are supported:
Architecture analysis
Cycle Detection (RFG-based)
Dead Code Detection (RFG-based)
Clone Detection (using C#CloneDetection)
C#-specific RFG transformations
RFG-based metrics
Stylechecks in the C# rule group
The following additional features are supported:
6.4.2.8. Frequently Asked Questions¶
I am able to build my project successfully using
MSBuild, but I am seeing various errors during execution ofcsharp2rfg. How do I solve this?Ensure that the correct version of
MSBuildis used; you can manually set themsbuild_pathoption to the correct version if auto-detection fails.Ensure that the correct configuration and platform are used. Unlike Visual Studio,
csharp2rfgmight not be able to infer them correctly; therefore, it is recommended to set them explicitly.If your project uses SDK-style projects and targets multiple frameworks, it may be necessary to choose one target framework for analysis, as an analysis cannot use multiple target frameworks. You can use the
target_frameworkoption (build_target_frameworkin the UI) to specify one explicitly.