Namotion.Reflection 3.3.0

dotnet add package Namotion.Reflection --version 3.3.0
                    
NuGet\Install-Package Namotion.Reflection -Version 3.3.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.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Namotion.Reflection" Version="3.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Namotion.Reflection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Namotion.Reflection --version 3.3.0
                    
#r "nuget: Namotion.Reflection, 3.3.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.
#addin nuget:?package=Namotion.Reflection&version=3.3.0
                    
Install Namotion.Reflection as a Cake Addin
#tool nuget:?package=Namotion.Reflection&version=3.3.0
                    
Install Namotion.Reflection as a Cake Tool

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.  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. 
.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 (53)

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 18 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
RicoSuter/NJsonSchema
JSON Schema reader, generator and validator for .NET
dotnetcore/sharding-core
high performance lightweight solution for efcore sharding table and sharding database support read-write-separation .一款ef-core下高性能、轻量级针对分表分库读写分离的解决方案,具有零依赖、零学习成本、零业务代码入侵
notifo-io/notifo
Multi channel notification service for collaboration tools, e-commerce, news service and more.
Coldairarrow/EFCore.Sharding
Database Sharding For EFCore
flyingpie/windows-terminal-quake
Enable Quake-style dropdown for (almost) any application.
bilal-fazlani/commanddotnet
A modern framework for building modern CLI apps
zarusz/SlimMessageBus
Lightweight message bus interface for .NET (pub/sub and request-response) with transport plugins for popular message brokers.
RicoSuter/VisualJsonEditor
A JSON schema based file editor for Windows.
dymproject/purest-admin
基于 .NET 8 + vue3 实现的极简RBAC权限管理系统后端 后端基于精简后的abp框架,前端基于vue-pure-admin,前端极强的表格框架vxe-table,旨在打造一款最合适二次开发的极简.Net框架
asyncapi/saunter
Saunter is a code-first AsyncAPI documentation generator for dotnet.
compomics/ThermoRawFileParser
Thermo RAW file parser that runs on Linux/Mac and all other platforms that support Mono
jburzynski/TypeGen
Single-class-per-file C# to TypeScript generator
admin-shell-io/aasx-package-explorer
C# based viewer / editor for the Asset Administration Shell
DigitalPlatform/dp2
Integrated Library System / 图书馆集成系统
unchase/Unchase.OpenAPI.Connectedservice
:scroll: Visual Studio extension to generate OpenAPI (Swagger) web service reference.
Version Downloads Last updated
3.3.0 175,076 a month ago
3.2.0 5,193,031 5 months ago
3.1.1 23,821,210 12/8/2023
3.1.0 519,359 10/31/2023
3.0.1 17,885 10/26/2023
3.0.0 287,508 8/20/2023
2.1.2 14,786,351 5/3/2023
2.1.1 1,232,817 9/18/2022
2.1.0 17,190,770 9/16/2022
2.0.10 33,147,325 1/27/2022
2.0.9 6,114,254 12/15/2021
2.0.8 2,591,334 12/9/2021
2.0.7 198,416 12/7/2021
2.0.6 7,140 12/5/2021
2.0.5 703,009 10/25/2021
2.0.4 959 10/25/2021
2.0.3 10,404,573 8/6/2021
2.0.2 912 8/6/2021
2.0.1 51,107 8/5/2021
2.0.0 24,911 8/4/2021
1.0.23 6,039,084 5/7/2021
1.0.22 30,252 5/5/2021
1.0.21 808 5/5/2021
1.0.20 2,094 5/4/2021
1.0.19 1,885,612 4/8/2021
1.0.18 4,375,950 2/4/2021
1.0.17 943 2/4/2021
1.0.16 7,995 1/29/2021
1.0.15 1,839,061 12/17/2020
1.0.14 7,234,782 9/28/2020
1.0.13 2,121,436 9/1/2020
1.0.12 144,631 5/30/2020
1.0.11 8,906,517 3/20/2020
1.0.10 507,874 2/20/2020
1.0.9 1,362 2/18/2020
1.0.8 3,552,441 1/17/2020
1.0.7 3,915,194 10/9/2019
1.0.6 1,935,473 8/2/2019
1.0.5 1,465,811 6/6/2019
1.0.4 96,580 5/26/2019
1.0.3 1,088 5/25/2019
1.0.2.4-build.20190522.4 564 5/22/2019
1.0.2 86,257 5/22/2019
1.0.1 18,709 5/21/2019
1.0.0 8,999 5/20/2019