TypeLogic.LiskovWingSubstitution
1.0.0
dotnet add package TypeLogic.LiskovWingSubstitution --version 1.0.0
NuGet\Install-Package TypeLogic.LiskovWingSubstitution -Version 1.0.0
<PackageReference Include="TypeLogic.LiskovWingSubstitution" Version="1.0.0" />
<PackageVersion Include="TypeLogic.LiskovWingSubstitution" Version="1.0.0" />
<PackageReference Include="TypeLogic.LiskovWingSubstitution" />
paket add TypeLogic.LiskovWingSubstitution --version 1.0.0
#r "nuget: TypeLogic.LiskovWingSubstitution, 1.0.0"
#:package TypeLogic.LiskovWingSubstitution@1.0.0
#addin nuget:?package=TypeLogic.LiskovWingSubstitution&version=1.0.0
#tool nuget:?package=TypeLogic.LiskovWingSubstitution&version=1.0.0
TypeLogic.LiskovWingSubstitution
A .NET library implementing the Liskov/Wing Substitution Principle for type variance checking (including generic typeParameters constraints validation).
This project emerged from previous R&D tests dealing with .NET generics: I needed an additional simple way to verify type variance against GenericTypeDefinition, complementing .NET's built-in type system capabilities.
What started as a proof-of-concept to simplify variance checking turned into a full-fledged library that extends .NET's type system that you may find educational or even practicalin some cases. The implementation is based from Barbara Liskov's and Jeannette Wing's work on behavioral subtyping, providing additional variance checking capabilities when working with GenericTypeDefinitions (especially in cases where non-generic Type inheritance is absent, as illustrated in the examples).
Features
- Type variance checking according to Liskov/Wing Substitution Principle
- Support for generic type parameter constraints
- Caching of type variance relationships for performance
- Instance-based variance checking
- Support for generic type definitions
- Full support for .NET Standard 2.0
Installation
The package can be installed via NuGet:
dotnet add package TypeLogic.LiskovWingSubstitution
Usage
Basic Type Variance Checking
using TypeLogic.LiskovWingSubstitution;
// Check if List<string> is variant of IEnumerable<object>
bool isVariant = typeof(List<string>).IsSubtypeOf(typeof(IEnumerable<object>));
// Check with runtime type information
bool isVariantWithType = typeof(List<string>).IsSubtypeOf(typeof(IEnumerable<object>), out Type runtimeType);
Instance-Based Variance Checking
using TypeLogic.LiskovWingSubstitution;
List<string> instance = new List<string>();
// Check with type parameter
bool isInstanceOfType = instance.IsInstanceOf(typeof(IEnumerable<object>), out var runtimeType);
// runtimeType should be IEnumerable<string>
using TypeLogic.LiskovWingSubstitution;
string instance = "this is a string";
// Check with against generic type with the corresponding runtimeType
bool isInstanceOfType = instance.IsInstanceOf(typeof(IEquatable<>), out var runtimeType);
// runtimeType should be IEquatable<string>
Generic Type Definition Support
using TypeLogic.LiskovWingSubstitution;
// Check if List<T> is variant of IEnumerable<>
bool isGenericVariant = typeof(List<int>).IsSubtypeOf(typeof(IEnumerable<>));
// Convert instance based on generic definition
string instance = "test";
var converted = instance.ConvertAs(typeof(IEnumerable<>)); // Converts to IEnumerable<char>
Features in Detail
Type Variance Checking
The library implements comprehensive type variance checking:
- Direct type equality
- Interface and inheritance hierarchy traversal
- Generic type parameter constraints validation
- Support for generic type definitions
- Cached type relationship checks
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Where to get help
- Open an issue in the repository with detailed steps to reproduce any bug or performance regression.
- For performance changes, include benchmark output (use
tools/compare-benchmarks.ps1when relevant).
References
- Barbara Liskov � Liskov Substitution Principle (overview): https://en.wikipedia.org/wiki/Liskov_substitution_principle
- Barbara Liskov and Jeannette M. Wing � "A Behavioral Notion of Subtyping" (1994) PDF: https://www.cs.cmu.edu/~wing/publications/LiskovWing94.pdf
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.