gitlabEdit

lock-keyholeAssembly Reflection

Reflecting things...

Nitrocid KS provides you with reflection tools to make using it easier for mods that want to use this feature for the following reasons:

  • Deep manipulation with the arrays

  • Assembly lookup management

  • Field, property, and method management

  • Integer manipulation

  • Common reflection tools

This feature of Nitrocid KS is essential in cases where the default .NET implementation doesn't implement features that are not normally available for all the supported frameworks, such as array randomization that isn't available until .NET 8.0.

This feature can also be used in future versions of Nitrocid KS to emulate features that have been implemented in future .NET versions and to do unusual tricks with various types, such as swapping two integers.

The following reflection tools are available:

chevron-rightArray manipulationhashtag

The reflection part of Nitrocid KS contains tools that allow you to manipulate with the arrays, such as sorting and randomizing array elements.

Here are the following functions that you can use:

Description

RandomizeArray()

Randomizes the array using the Schwartzian's transformarrow-up-right formula.

RandomizeArraySystem()

Randomizes the array using .NET 8.0's Random.Shared.Shuffle()arrow-up-right function.

SortNumbers()

Sorts the numbers using one of the sorting algorithms implemented by your array sorting drivers.

chevron-rightAssembly lookup managementhashtag

Nitrocid KS contains the assembly lookup tools to manage how the kernel will load the assemblies specified in the search paths that AssemblyLookup manages. It allows you to add and to remove a path to/from the lookup path list using the following functions:

  • AddPathToAssemblySearchPath()

  • RemovePathFromAssemblySearchPath()

To load an assembly from the search paths, such as your mod dependencies, Nitrocid adds an event handler to the AppDomain.CurrentDomain.AssemblyResolvearrow-up-right event to be able to call our assembly path resolver in an attempt to load any and all dependencies for your mods.

circle-info

In case Nitrocid attempts to load an old mod that depends on the old name of the application, Kernel Simulator, it'll give you an error saying that the mod is too old to be loadable. Therefore, mod loading will fail. If you're experiencing this in one of your mods and believe that this is the reason, contact the mod publisher for details about how to get the latest version.

chevron-rightInteger toolshashtag

The Reflection part of the kernel contains a class, IntegerTools, that consists of useful integer tools from short numbers to 128-bit integer numbers to double-precision numbers, like converting literal file sizes in bytes to their human formats.

In addition to that, we've also placed useful extensions, such as converting the number of all kinds, short or long, to different number bases as strings, such as:

  • ToBinary()

  • ToOctal()

  • ToNumber()

  • ToHex()

chevron-rightFile size conversionhashtag

SizeString() extension functions can be used when importing the Reflection namespace. This allows you to easily convert the file sizes from bytes to their human-readable format.

You can use these functions, once the above namespace is imported, like this:

circle-exclamation
chevron-rightProperties and Fieldshashtag

The Reflection part of the Nitrocid API provides you with options to access public properties and fields that are declared in the public classes dynamically.

  • PropertyManager manages properties, such as getting and setting property values.

  • FieldManager manages fields, such as getting and setting field values.

In addition to the functions available in the above two classes, you can get all fields and properties defined in all the kernel types using the following functions:

  • GetAllFields()

  • GetAllFieldsNoEvaluation()

  • GetAllProperties()

  • GetAllPropertiesNoEvaluation()

chevron-rightMethod toolshashtag

In addition to property and field tools, the reflection feature of Nitrocid KS also allows you to manage methods and invoke them by their name or by their MethodBase instance.

This set of tools allows you to do the following:

Function
Description

GetMethod()

This function allows you to get a public method from either one of the Nitrocid kernel types or a specific type inside and outside Nitrocid.

InvokeMethod()

This function allows you to invoke a non-static method, provided that you already have an object that is of a specific type that implements the public method.

InvokeMethodStatic()

This function allows you to invoke a public static method.

chevron-rightCommon reflection toolshashtag

The common reflection tools are here to help you get the most out of the reflection feature of .NET by providing you the following functions:

Function
Description

IsDotnetAssemblyFile()

This function checks to see if a path to a specific file is a .NET assembly or not. If it is a .NET assembly, it returns true with an AssemblyName instance containing information about your desired assembly.

GetType()

This function gets a type from different assembly contexts that were added when loading addons or mods.

Last updated