Skip to main content
Covarsky package icon

Covarsky

v1.4.3Latest Stable
.NETStandard2.0

Provides support for declaring covariant and contravariant generic parameters for languages that lack native support, like F#.

Published
Mar 26, 2025
1y ago
Total Downloads
15.2K
15,215

Install

dotnet add package Covarsky --version 1.4.3
ProductVersions
.NET Standard
netstandard2.0

README

Licensed under the MIT License NuGet CI build status

Covarsky

Covarsky is a tool that brings covariant and contravariant generic types to .ΝΕΤ languages that do not natively support them like F#. Powered by Sigourney, it runs an MSBuild task that modifies the assembly after compilation.

How to use

  1. Add the Covarsky NuGet package to your project.

  2. Create a new source file with two (or one, if you only need one kind of variance) attributes like that (F# example shown):

namespace global

open System

[<AttributeUsage(AttributeTargets.GenericParameter)>]
type internal CovariantOutAttribute() =
    inherit Attribute()

[<AttributeUsage(AttributeTargets.GenericParameter)>]
type internal ContravariantInAttribute() =
    inherit Attribute()

Warning: The attributes must be declared in the global namespace, directly derive from the Attribute class and be internal.

  1. You are good to go! Let's see an example.
type MyType<[<CovariantOut>] 'T> =
    abstract GetObject: unit -> 'T

// [...]

let cast (x: MyType<string>): MyType<obj> =
    unbox x

As you have seen, we have to perform manual type casts using unbox because F# does not recognize that MyType's 'T is covariant.

Customizing Covarsky

Using custom attribute names

If for any reason you want to customize the attribute names Covarsky will recognize, you can do it by specifying it in your project file:

<PropertyGroup>
    <CustomInAttributeName>MyLibrary.MakeItContravariantPleaseAttribute</CustomInAttributeName>
    <CustomOutAttributeName>MyLibrary.MakeItCovariantPleaseAttribute</CustomOutAttributeName>
</PropertyGroup>

As you see, fully qualified names are accepted. The attribute classes however still have to be internal and belong to the same assembly.

Disabling Covarsky

Since version 1.3.0 Covarsky is enabled by default when you install the package. If for any reason you want to disable it you can do it with the following property:

<PropertyGroup>
    <CovarskyEnable>false</CovarskyEnable>
</PropertyGroup>

Caveats

  • After Covarsky's execution, the two attributes will not be removed. Since version 1.4.0 the attributes will be removed from the generic parameters, allowing them to be trimmed by the IL Linker. User code should not rely on their existence.

  • These two attributes will be ignored if used anywhere but in the generic parameters of an interface or a delegate.

  • Using these two attributes in a generic parameter that is already variant will raise a warning but will be ignored as well.

  • Using both attributes on the same generic parameter will raise an error and fail the build (unless something above hasn't already happened).

  • Using the attributes in any other wrong way (such as a contravariant interface with a method that returns the generic type) will not be checked by Covarsky and may break your assembly.

Documentation

Maintainer(s)

Changes from Previous Version

No structural changes between v1.4.2 and v1.4.3.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.