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 (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 (1)
Showing the top 1 popular GitHub repositories that depend on Namotion.Reflection:
Repository | Stars |
---|---|
chocolatey/choco
Chocolatey - the package manager for Windows
|
Version | Downloads | Last updated |
---|---|---|
3.2.0 | 9,222 | 11/19/2024 |
3.1.1 | 15,423,856 | 12/8/2023 |
3.1.0 | 426,217 | 10/31/2023 |
3.0.1 | 14,060 | 10/26/2023 |
3.0.0 | 259,974 | 8/20/2023 |
2.1.2 | 12,930,449 | 5/3/2023 |
2.1.1 | 1,167,954 | 9/18/2022 |
2.1.0 | 16,075,904 | 9/16/2022 |
2.0.10 | 28,873,953 | 1/27/2022 |
2.0.9 | 5,864,638 | 12/15/2021 |
2.0.8 | 1,900,861 | 12/9/2021 |
2.0.7 | 194,506 | 12/7/2021 |
2.0.6 | 6,982 | 12/5/2021 |
2.0.5 | 684,294 | 10/25/2021 |
2.0.4 | 817 | 10/25/2021 |
2.0.3 | 9,674,693 | 8/6/2021 |
2.0.2 | 767 | 8/6/2021 |
2.0.1 | 49,202 | 8/5/2021 |
2.0.0 | 24,483 | 8/4/2021 |
1.0.23 | 5,615,450 | 5/7/2021 |
1.0.22 | 29,542 | 5/5/2021 |
1.0.21 | 722 | 5/5/2021 |
1.0.20 | 2,022 | 5/4/2021 |
1.0.19 | 1,792,298 | 4/8/2021 |
1.0.18 | 4,242,597 | 2/4/2021 |
1.0.17 | 865 | 2/4/2021 |
1.0.16 | 7,760 | 1/29/2021 |
1.0.15 | 1,733,328 | 12/17/2020 |
1.0.14 | 6,870,369 | 9/28/2020 |
1.0.13 | 2,059,970 | 9/1/2020 |
1.0.12 | 142,093 | 5/30/2020 |
1.0.11 | 8,635,348 | 3/20/2020 |
1.0.10 | 499,767 | 2/20/2020 |
1.0.9 | 1,283 | 2/18/2020 |
1.0.8 | 3,459,748 | 1/17/2020 |
1.0.7 | 3,845,703 | 10/9/2019 |
1.0.6 | 1,858,058 | 8/2/2019 |
1.0.5 | 1,416,129 | 6/6/2019 |
1.0.4 | 94,732 | 5/26/2019 |
1.0.3 | 1,004 | 5/25/2019 |
1.0.2.4-build.20190522.4 | 498 | 5/22/2019 |
1.0.2 | 85,503 | 5/22/2019 |
1.0.1 | 18,612 | 5/21/2019 |
1.0.0 | 8,497 | 5/20/2019 |