Advertisement
Ansjh

Untitled

Nov 28th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace Mrag2.Hacks
  5. {
  6.   /// <summary>
  7.   /// Class that extends the object class with methods that allow you to interact with private members of said objects.
  8.   /// </summary>
  9.   public static class Hacks
  10.   {
  11.     // Helpers
  12.     private static FieldInfo hackFieldInfo(this object obj, string name)
  13.     {
  14.       return obj.GetType().GetField(name, (BindingFlags)65535);
  15.     }
  16.  
  17.     private static PropertyInfo hackPropertyInfo(this object obj, string name)
  18.     {
  19.       return obj.GetType().GetProperty(name, (BindingFlags)65535);
  20.     }
  21.  
  22.     private static MethodInfo hackMethodInfo(this object obj, string name)
  23.     {
  24.       return obj.GetType().GetMethod(name, (BindingFlags)65535);
  25.     }
  26.  
  27.     // Fields
  28.     public static void HackSetField(this object obj, string name, object value)
  29.     {
  30.       FieldInfo fi = obj.hackFieldInfo(name);
  31.       if (fi != null) fi.SetValue(obj, value);
  32.     }
  33.  
  34.     public static object HackGetField(this object obj, string name)
  35.     {
  36.       FieldInfo fi = obj.hackFieldInfo(name);
  37.       if (fi != null) return fi.GetValue(obj);
  38.       return null;
  39.     }
  40.  
  41.     // Properties
  42.     public static void HackSetProperty(this object obj, string name, object value)
  43.     {
  44.       PropertyInfo pi = obj.hackPropertyInfo(name);
  45.       if (pi != null) pi.SetValue(obj, value, null);
  46.     }
  47.  
  48.     public static void HackSetProperty(this object obj, string name, object value, object index)
  49.     {
  50.       PropertyInfo pi = obj.hackPropertyInfo(name);
  51.       if (pi != null) pi.SetValue(obj, value, new object[] { index });
  52.     }
  53.  
  54.     public static void HackSetProperty(this object obj, string name, object value, object[] index)
  55.     {
  56.       PropertyInfo pi = obj.hackPropertyInfo(name);
  57.       if (pi != null) pi.SetValue(obj, value, index);
  58.     }
  59.  
  60.     public static object HackGetProperty(this object obj, string name)
  61.     {
  62.       PropertyInfo pi = obj.hackPropertyInfo(name);
  63.       if (pi != null) return pi.GetValue(obj, null);
  64.       return null;
  65.     }
  66.  
  67.     public static object HackGetProperty(this object obj, string name, object index)
  68.     {
  69.       PropertyInfo pi = obj.hackPropertyInfo(name);
  70.       if (pi != null) return pi.GetValue(obj, new object[] { index });
  71.       return null;
  72.     }
  73.  
  74.     public static object HackGetProperty(this object obj, string name, object[] index)
  75.     {
  76.       PropertyInfo pi = obj.hackPropertyInfo(name);
  77.       if (pi != null) return pi.GetValue(obj, index);
  78.       return null;
  79.     }
  80.  
  81.     // Methods
  82.     public static object HackCallMethod(this object obj, string name, params object[] args)
  83.     {
  84.       MethodInfo mi = obj.hackMethodInfo(name);
  85.       if (mi != null) return mi.Invoke(obj, args);
  86.       return null;
  87.     }
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement