Skip to content

Instantly share code, notes, and snippets.

@TheWover
Last active January 25, 2020 21:09
Show Gist options
  • Save TheWover/40b067ffdc617fbb1043cab4eeeb10eb to your computer and use it in GitHub Desktop.
Save TheWover/40b067ffdc617fbb1043cab4eeeb10eb to your computer and use it in GitHub Desktop.
Forces the abstract subclass's overload to be called. Dis sketch.
/// <summary>
/// Allocate the payload to the target process at a specified address.
/// </summary>
/// <param name="payload">The payload to allocate to the target process.</param>
/// <param name="process">The target process.</param>
/// <param name="address">The address at which to allocate the payload in the target process.</param>
/// <returns>True when allocation was successful. Otherwise, throws relevant exceptions./returns>
public IntPtr Allocate(PayloadType payload, System.Diagnostics.Process process, IntPtr address)
{
//Create the function prototype (signature) for the function we will call in the subclass
Type[] funcPrototype = new Type[] { payload.GetType(), typeof(System.Diagnostics.Process), address.GetType() };
try
{
//Get delegate to the overload of Allocate that supports the type of payload passed in
//Use of the this object ensures that GetMethod is called on the *runtime type*, rather than this parent class
System.Reflection.MethodInfo allocate = this.GetType().GetMethod("Allocate", funcPrototype);
//Dynamically invoke the appropriate Allocate overload
return (IntPtr)allocate.Invoke(this, new object[] { payload, process, address });
}
//If there is no such method
catch (ArgumentNullException)
{
throw new PayloadTypeNotSupported(payload.GetType());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment