Skip to content

Utilities

Andreas Pardeike edited this page Jan 13, 2017 · 11 revisions

Traverse

In order to access fields, properties and methods from classes via reflection, Harmony contains a utility called Traverse. Think of it as LINQ for classes.

Example:

class Foo
{
	struct Bar
	{
		static string secret = "hello";

		public string ModifiedSecret()
		{
			return secret.ToUpper();
		}
	}

	Bar myBar
	{
		get
		{
			return new Bar();
		}
	}

	public string GetSecret()
	{
		return myBar.ModifiedSecret();
	}

	Foo()
	{
	}

	static Foo MakeFoo()
	{
		return new Foo();
	}
}

var foo = Traverse.Create<Foo>().Method("MakeFoo").GetValue<Foo>();
Traverse.Create(foo).Property("myBar").Field("secret").SetValue("world");
Console.WriteLine(foo.GetSecret()); // outputs WORLD

Although most fields, properties and methods in that class hierarchy are private, Traverse can easily access anything. It has build-in null protection and propagates null as a result if any of the intermediates would encounter null. It works with static types and caches lookups which makes it pretty fast.