Namotion.Reflection 3.2.0

dotnet add package Namotion.Reflection --version 3.2.0                
NuGet\Install-Package Namotion.Reflection -Version 3.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Namotion.Reflection" Version="3.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Namotion.Reflection --version 3.2.0                
#r "nuget: Namotion.Reflection, 3.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Namotion.Reflection as a Cake Addin
#addin nuget:?package=Namotion.Reflection&version=3.2.0

// Install Namotion.Reflection as a Cake Tool
#tool nuget:?package=Namotion.Reflection&version=3.2.0                

Namotion.Reflection

Storage | Messaging | Reflection

Azure DevOps Azure DevOps Nuget Discord

<img align="left" src="https://raw.githubusercontent.com/RicoSuter/Namotion.Reflection/master/assets/Icon.png" width="48px" height="48px">

.NET library with advanced reflection APIs like XML documentation reading, Nullable Reference Types (C# 8) reflection and string based type checks.

This library is mainly used in NJsonSchema and NSwag.

Contextual and cached types

Inheritance hierarchy:

  • CachedType: A cached Type object which does not have a context
    • ContextualType: A cached Type with contextual attributes (e.g. property attributes)
      • ContextualParameterInfo
      • ContextualMemberInfo
        • ContextualMethodInfo
        • ContextualAccessorInfo
          • ContextualPropertyInfo
          • ContextualFieldInfo

Behavior:

  • Each CachedType instance is cached per Type, ParameterInfo or MemberInfo.
  • Contextual and type attributes are evaluated only once and then cached for higher performance.
  • If the original Type is Nullable<T> then T is unwrapped and stored in the Type property - the original type can be accessed with the OriginalType property.

Nullability reflection (C# 8)

With the ContextualType class you can reflect on the nullability of properties, fields, method parameters and return types which will be available when compiling with the C# 8 compiler with the Nullable Reference Types feature enabled.

Given the following test class with some C# 8 nullability annotations (?):

#nullable enable

public class MyClass
{
    public void MyMethod(Dictionary<string, string?> dictionary)
    {
    }
}

To reflect on the first parameter's nullability, we can load a ContextualType instance and display the nullability of the parameter's types:

using Namotion.Reflection;

var method = typeof(MyClass).GetMethod(nameof(MyClass.MyMethod));
var parameter = method.GetParameters().First();
var contextualParameter = parameter.ToContextualParameter();

Console.WriteLine("Dictionary: " + contextualParameter.Nullability);
Console.WriteLine("Key: " + contextualParameter.GenericArguments[0].Nullability);
Console.WriteLine("Value: " + contextualParameter.GenericArguments[1].Nullability);

The output is:

Dictionary: NotNullable
Key: NotNullable
Value: Nullable

For more details, see https://blog.rsuter.com/the-output-of-nullable-reference-types-and-how-to-reflect-it/

Methods:

  • CachedType.ClearCache()

Validate nullability (C# 8)

It is important to understand that Nullable Reference Types is a compiler feature only and the .NET runtime does not do any checks when your app is running. Consider the following class:

public class Person
{
    public string FirstName { get; set; }

    public string? MiddleName { get; set; }

    public string LastName { get; set; }
}

Inside your application you'll get warnings when you forget to set the FirstName. However when data is coming from outside (e.g. via reflection, serialization, etc.) you could end up with invalid objects. This JSON.NET call throws no exception but will create an invalid object:

var person = JsonConvert.DeserializeObject<Person>("{}");

Call the EnsureValidNullability() extension method which throws an InvalidOperationException when the object is in an invalid state:

person.EnsureValidNullability();

Methods:

  • HasValidNullability();
  • EnsureValidNullability();
  • ValidateNullability();

Read XML Documentation

Methods:

  • Type|MemberInfo.GetXmlDocsSummaryAsync():

  • Type|MemberInfo.GetXmlDocsRemarksAsync():

  • ParameterInfo.GetXmlDocsAsync(): Gets the parameter's description

  • ParameterInfo.GetXmlDocsElementAsync(): Gets the XElement of the given type

  • ... and more

  • XmlDocs.ClearCache()

This functionality can also be used with Cecil types with the Namotion.Reflection.Cecil package.

Extension methods

Methods:

IEnumerable extensions

  • GetAssignableToTypeName(): Gets all objects which are assignable to the given type name as string.
  • FirstAssignableToTypeNameOrDefault(): Tries to get the first object which is assignable to the given type name as string.
  • GetCommonBaseType(): Finds the first common base type of the given types.

Object extensions

  • HasProperty(): Determines whether the specified property name exists.
  • TryGetPropertyValue(): Returns the value of the given property or null if the property does not exist.

Type extensions

  • IsAssignableToTypeName()
  • InheritsFromTypeName()
  • GetEnumerableItemType()
  • GetDisplayName(): Gets a human readable identifier for the type (eg. "DictionaryOfStringAndInt32").
Product 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 is compatible.  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. 
.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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (52)

Showing the top 5 NuGet packages that depend on Namotion.Reflection:

Package Downloads
NJsonSchema

JSON Schema reader, generator and validator for .NET

TypeGen

TypeGen is a single-class-per-file C# to TypeScript generator

Dynamicweb.Admin

Package Description

KubeOps

This is an operator sdk written in c#. It enables a developer to create a custom controller for CRDs (CustomResourceDefinitions) that runs on kubernetes.

EFCore.Sharding

Package Description

GitHub repositories (18)

Showing the top 5 popular GitHub repositories that depend on Namotion.Reflection:

Repository Stars
chocolatey/choco
Chocolatey - the package manager for Windows
RicoSuter/NSwag
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
KSP-CKAN/CKAN
The Comprehensive Kerbal Archive Network
WireMock-Net/WireMock.Net
WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
RicoSuter/NJsonSchema
JSON Schema reader, generator and validator for .NET
Version Downloads Last updated
3.2.0 527,423 11/19/2024
3.1.1 17,644,727 12/8/2023
3.1.0 445,810 10/31/2023
3.0.1 14,863 10/26/2023
3.0.0 266,832 8/20/2023
2.1.2 13,393,558 5/3/2023
2.1.1 1,186,499 9/18/2022
2.1.0 16,341,158 9/16/2022
2.0.10 29,820,481 1/27/2022
2.0.9 5,942,048 12/15/2021
2.0.8 2,073,425 12/9/2021
2.0.7 195,272 12/7/2021
2.0.6 6,998 12/5/2021
2.0.5 688,167 10/25/2021
2.0.4 831 10/25/2021
2.0.3 9,825,087 8/6/2021
2.0.2 785 8/6/2021
2.0.1 49,666 8/5/2021
2.0.0 24,557 8/4/2021
1.0.23 5,726,200 5/7/2021
1.0.22 29,666 5/5/2021
1.0.21 742 5/5/2021
1.0.20 2,039 5/4/2021
1.0.19 1,813,996 4/8/2021
1.0.18 4,273,104 2/4/2021
1.0.17 881 2/4/2021
1.0.16 7,809 1/29/2021
1.0.15 1,756,084 12/17/2020
1.0.14 6,948,307 9/28/2020
1.0.13 2,074,602 9/1/2020
1.0.12 142,630 5/30/2020
1.0.11 8,691,599 3/20/2020
1.0.10 501,727 2/20/2020
1.0.9 1,301 2/18/2020
1.0.8 3,482,498 1/17/2020
1.0.7 3,863,197 10/9/2019
1.0.6 1,876,057 8/2/2019
1.0.5 1,425,864 6/6/2019
1.0.4 95,188 5/26/2019
1.0.3 1,025 5/25/2019
1.0.2.4-build.20190522.4 508 5/22/2019
1.0.2 85,786 5/22/2019
1.0.1 18,633 5/21/2019
1.0.0 8,640 5/20/2019