Namotion.Reflection
3.2.0
dotnet add package Namotion.Reflection --version 3.2.0
NuGet\Install-Package Namotion.Reflection -Version 3.2.0
<PackageReference Include="Namotion.Reflection" Version="3.2.0" />
paket add Namotion.Reflection --version 3.2.0
#r "nuget: Namotion.Reflection, 3.2.0"
// 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
<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
- ContextualType: A cached
Behavior:
- Each
CachedType
instance is cached perType
,ParameterInfo
orMemberInfo
. - Contextual and type attributes are evaluated only once and then cached for higher performance.
- If the original
Type
isNullable<T>
thenT
is unwrapped and stored in theType
property - the original type can be accessed with theOriginalType
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 | 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 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. |
-
.NETFramework 4.6.2
- Microsoft.CSharp (>= 4.7.0)
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
-
net8.0
- No dependencies.
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 | 534,131 | 11/19/2024 |
3.1.1 | 17,660,073 | 12/8/2023 |
3.1.0 | 445,960 | 10/31/2023 |
3.0.1 | 14,884 | 10/26/2023 |
3.0.0 | 266,887 | 8/20/2023 |
2.1.2 | 13,397,295 | 5/3/2023 |
2.1.1 | 1,186,632 | 9/18/2022 |
2.1.0 | 16,343,123 | 9/16/2022 |
2.0.10 | 29,827,481 | 1/27/2022 |
2.0.9 | 5,942,555 | 12/15/2021 |
2.0.8 | 2,075,267 | 12/9/2021 |
2.0.7 | 195,295 | 12/7/2021 |
2.0.6 | 6,998 | 12/5/2021 |
2.0.5 | 688,201 | 10/25/2021 |
2.0.4 | 831 | 10/25/2021 |
2.0.3 | 9,826,102 | 8/6/2021 |
2.0.2 | 785 | 8/6/2021 |
2.0.1 | 49,671 | 8/5/2021 |
2.0.0 | 24,557 | 8/4/2021 |
1.0.23 | 5,727,078 | 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,814,188 | 4/8/2021 |
1.0.18 | 4,273,291 | 2/4/2021 |
1.0.17 | 883 | 2/4/2021 |
1.0.16 | 7,811 | 1/29/2021 |
1.0.15 | 1,756,329 | 12/17/2020 |
1.0.14 | 6,948,751 | 9/28/2020 |
1.0.13 | 2,074,667 | 9/1/2020 |
1.0.12 | 142,630 | 5/30/2020 |
1.0.11 | 8,692,006 | 3/20/2020 |
1.0.10 | 501,758 | 2/20/2020 |
1.0.9 | 1,301 | 2/18/2020 |
1.0.8 | 3,482,698 | 1/17/2020 |
1.0.7 | 3,863,326 | 10/9/2019 |
1.0.6 | 1,876,186 | 8/2/2019 |
1.0.5 | 1,425,966 | 6/6/2019 |
1.0.4 | 95,205 | 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,795 | 5/22/2019 |
1.0.1 | 18,633 | 5/21/2019 |
1.0.0 | 8,640 | 5/20/2019 |