ApacheTech.Common.Extensions.Harmony 2.1.0

dotnet add package ApacheTech.Common.Extensions.Harmony --version 2.1.0
                    
NuGet\Install-Package ApacheTech.Common.Extensions.Harmony -Version 2.1.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="ApacheTech.Common.Extensions.Harmony" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApacheTech.Common.Extensions.Harmony" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ApacheTech.Common.Extensions.Harmony" />
                    
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 ApacheTech.Common.Extensions.Harmony --version 2.1.0
                    
#r "nuget: ApacheTech.Common.Extensions.Harmony, 2.1.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.
#:package ApacheTech.Common.Extensions.Harmony@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApacheTech.Common.Extensions.Harmony&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ApacheTech.Common.Extensions.Harmony&version=2.1.0
                    
Install as a Cake Tool

Harmony Reflection Extensions

This package provides a set of extension methods that make advanced reflection in C# simple and elegant, especially for accessing internal and private members. These methods are designed to work at the object level, allowing you to get, set, and invoke fields, properties, and methods (even those not normally accessible) using the power of Harmony's AccessTools.

What is Harmony?
Harmony is a popular library for patching, replacing, and modifying .NET methods at runtime. This extension package leverages Harmony's robust reflection utilities to provide a more convenient API for everyday use.

Compatibility:

  • .NET 8 or later
  • Requires Lib.Harmony (not included)

Quick Example:

using ApacheTech.Common.Extensions.Harmony;

// Access a private field
var value = myObject.GetField<int>("_privateField");

// Set a private property
myObject.SetProperty("InternalProperty", 42);

// Call a private method
myObject.CallMethod("HiddenMethod");

All that is required is that Lib.Harmony should be included within your project. This package does not supply Harmony. Once installed, add the following using statement to your class file.

using ApacheTech.Common.Extensions.Harmony;

Support the Author

If you find this library useful, and you would like to show appreciation for the work I produce; please consider supporting me, and my work, using one of the methods below. Every single expression of support is most appreciated, and makes it easier to produce updates, and new features for my libraries, moving fowards. Thank you.

Extension Methods

The following tables list all extension methods provided by this library, grouped by category. Each entry includes the method signature and a brief description of its functionality.

Fields

Method Description
T? GetField<T>(this object instance, string fieldName) Gets a field within the calling instanced object. This can be an internal or private field within another assembly.
T?[]? GetFields<T>(this object instance) Gets an array of fields within the calling instanced object, of a specified type. These can be internal or private fields within another assembly.
void SetField(this object instance, string fieldName, object setVal) Sets a field within the calling instanced object. This can be an internal or private field within another assembly.

Properties

Method Description
T? GetProperty<T>(this object instance, string propertyName) Gets a property within the calling instanced object. This can be an internal or private property within another assembly.
void SetProperty(this object instance, string propertyName, object setVal) Sets a property within the calling instanced object. This can be an internal or private property within another assembly.
T? GetStaticProperty<T>(this Type type, string propertyName) Gets a static property within the calling type. This can be an internal or private property within another assembly.
void SetStaticProperty(this Type type, string propertyName, object setVal) Sets a static property within the calling type. This can be an internal or private property within another assembly.
T?[]? GetProperties<T>(this object instance) Gets an array of properties within the calling instanced object, of a specified type. These can be internal or private properties within another assembly.

Methods

Method Description
T? CallMethod<T>(this object instance, string method, params object[] args) Calls a method within an instance of an object, via reflection, and returns a value. This can be an internal or private method within another assembly.
void CallMethod(this object instance, string method, params object[] args) Calls a method within an instance of an object, via reflection. This can be an internal or private method within another assembly.
void CallMethod(this object instance, string method) Calls a method with no arguments within an instance of an object, via reflection.
void CallStaticMethod(this object instance, string method) Calls a static method within an object, via reflection.
void CallStaticMethod(this Type type, string method) Calls a static method within a type, via reflection.
MethodInfo GetMethod(this object instance, string method, Type[]? parameters = null, Type[]? generics = null) Gets the MethodInfo for a method within an instance of a class, via reflection.
void CallBaseMethod<TBaseClass>(this object instance, string method, params object[] args) Calls a base class method on an instance of an object via reflection.
TValue? CallBaseMethod<TBaseClass, TValue>(this object instance, string method, params object[] args) Calls a base class method on an instance of an object via reflection and returns its result.

Types

Method Description
object CreateInstance(this Type type) Creates the instance of a specified Type, using Harmony AccessTools. Be aware that this will ignore all Service Providers, and attempt to directly instantiate a class.
Type? GetClassType(this Assembly assembly, string className) Gets the type of the class within an assembly, via reflection.

Objects

Method Description
T DeepClone<T>(this T source) where T : class Makes a deep copy of any object.
void DeepClone<T>(this T source, out T copy) where T : class Makes a deep copy of any object and outputs the copy via an out parameter.
Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ApacheTech.Common.Extensions.Harmony:

Package Downloads
ApacheTech.VintageMods.FluentChatCommands

Provides a framework for creating client-side, and server-side chat commands, using a Fluent Builder pattern.

VintageStory.Gantry

Gantry MDK is a Mod Developent Kit, used to create third-party plugins for the game Vintage Story, by Anego Studios.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 120 9/14/2025
2.0.1 2,277 1/6/2023
2.0.0 432 11/15/2022
1.2.0 7,841 7/23/2022
1.1.0 424 12/14/2021

ApacheTech.Common.Extensions.Harmony v2.1.0

- Build: Updated to .NET 8.0 SDK.