Skip to content

Including Light.GuardClauses as source code

Kenny Pflug edited this page Nov 19, 2018 · 7 revisions

Light.GuardClauses is not only available as a NuGet package / DLL, but also as a single source code file. This should be especially interesting for framework / library developers that do want to use Light.GuardClauses in their projects but not depend on the DLL.

There are two ways to grab the sources.

Copying the source code of the .NET Standard 2.0 version

A default source code version is published as part of this repository. Simply copy the contents of Light.GuardClauses.SingleFile.cs in the top level directory of this repo. This file is always updated when a new version of Light.GuardClauses is released.

This default single-source code file has the following preferences:

  • All types use the internal modifier. This way the source code of Light.GuardClauses does not extend your own API
  • All preprocessor directives are removed, the source code is compliant with .NET Standard 2.0
  • ReSharper Contract Annotations are included

If this doesn't fit your needs, you can generate your custom single-source-code file.

Generating a custom single-source-code file

The Light.GuardClauses.SourceCodeTransformation project can be configured to customize the output of the single-source-code file. You can either create a JSON file called "settings.json" right besides the csproj file of this project, or you can configure the generator via command line arguments (the generator uses Microsoft.Extensions.Configuration internally). The default structure of this JSON file looks like this:

{
    "changePublicTypesToInternalTypes": true,
    "baseNamespace": "Light.GuardClauses",
    "removePreprocessorDirectives": true,
    "definedPreprocessorSymbols": ["NETSTANDARD2_0"],
    "removeContractAnnotations": false,
    "includeJetBrainsAnnotationsUsing": true,
    "includeJetBrainsAnnotations": true,
    "includeJetBrainsAnnotationsUsing": true,
    "includeVersionComment": true
}

Here is a description of all possible parameters (the names are case-insensitive):

Parameter Type Default Value Description
BaseNamespace string "Light.GuardClauses" Changes the namespace where all types of Light.GuardClauses will be part of.
ChangePublicTypesToInternalTypes bool true This value indicates whether all types should be changed from public to internal. This way, Light.GuardClauses will not extend your API.
RemovePreprocessorDirectives bool true Defines if preprocessor directives should be removed during generation. This should normally be set to true, especially when your project does not use any of the TargetFrameworks that Light.GuardClauses targets. You must set DefinedPreprocessorSymbols when this value is true.
DefinedPreprocessorSymbols string collection ["NETSTANDARD2_0"] The defined preprocessor symbols that are used to evaluate and remove the preprocessor directives. Usually, you should define a single symbol that corresponds to the framework you want to target (e.g. NETSTANDARD2_0 or NET45). The generator uses Light.Undefine under the covers.
RemoveContractAnnotations bool false This value indicates whether the ContractAnnotationAttributes should be removed from the assertions and methods of Light.GuardClauses. This attribute helps ReSharper analyzing possible null references.
IncludeJetBrainsAnnotations bool true This value indicates whether the definition of the ContractAnnotationAttribute should be included at the end of the generated source file.
IncludeJetBrainsAnnotationsUsing bool true This value indicates whether the using JetBrains.Annotations statement should be included at the top of the generated file.
IncludeVersionComment bool true This value indicates whether the version should be printed as a comment at the top of the generated file.
SourceFolder string Light.GuardClauses project folder The absolute or relative path to the folder that contains the Light.GuardClauses cs files. Usually, you do not have to set this value - it is dynamically resolved to the project folder that contains Light.GuardClauses.csproj
TargetFile string ..\Light.GuardClauses.Source\Light.GuardClauses.cs The absolute or relative path to the target file that will be generated. By default, the file will be written next to the Light.GuardClauses.Source.csproj file.
Clone this wiki locally