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 (51)

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

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

EachShow.Util

通用基础功能

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 104,646 11/19/2024
3.1.1 16,155,198 12/8/2023
3.1.0 432,533 10/31/2023
3.0.1 14,379 10/26/2023
3.0.0 262,287 8/20/2023
2.1.2 13,080,756 5/3/2023
2.1.1 1,174,681 9/18/2022
2.1.0 16,163,500 9/16/2022
2.0.10 29,171,065 1/27/2022
2.0.9 5,886,848 12/15/2021
2.0.8 1,956,157 12/9/2021
2.0.7 194,716 12/7/2021
2.0.6 6,991 12/5/2021
2.0.5 685,343 10/25/2021
2.0.4 824 10/25/2021
2.0.3 9,719,958 8/6/2021
2.0.2 778 8/6/2021
2.0.1 49,350 8/5/2021
2.0.0 24,510 8/4/2021
1.0.23 5,647,609 5/7/2021
1.0.22 29,607 5/5/2021
1.0.21 731 5/5/2021
1.0.20 2,029 5/4/2021
1.0.19 1,799,480 4/8/2021
1.0.18 4,252,156 2/4/2021
1.0.17 874 2/4/2021
1.0.16 7,771 1/29/2021
1.0.15 1,740,292 12/17/2020
1.0.14 6,896,726 9/28/2020
1.0.13 2,064,725 9/1/2020
1.0.12 142,270 5/30/2020
1.0.11 8,653,351 3/20/2020
1.0.10 500,457 2/20/2020
1.0.9 1,292 2/18/2020
1.0.8 3,466,271 1/17/2020
1.0.7 3,851,125 10/9/2019
1.0.6 1,863,322 8/2/2019
1.0.5 1,418,674 6/6/2019
1.0.4 94,881 5/26/2019
1.0.3 1,017 5/25/2019
1.0.2.4-build.20190522.4 504 5/22/2019
1.0.2 85,558 5/22/2019
1.0.1 18,626 5/21/2019
1.0.0 8,559 5/20/2019