Advertisement
mausha

Reflection_LAB

Aug 5th, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System;
  2.  
  3. public class StartUp
  4. {
  5.     public static void Main()
  6.     {
  7.         var spy = new Spy();
  8.         //var result = spy.StealFieldInfo("Hacker", "username", "password");
  9.         //var result = spy.AnalyzeAcessModifiers("Hacker");
  10.         //var result = spy.RevealPrivateMethods("Hacker");
  11.         var result = spy.CollectGettersAndSetters("Hacker");
  12.  
  13.         Console.WriteLine(result);
  14.     }
  15. }
  16.  
  17. using System;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Text;
  21.  
  22. public class Spy
  23. {
  24.     private StringBuilder result;
  25.     private Type classType;
  26.  
  27.     public Spy()
  28.     {
  29.         this.result = new StringBuilder();
  30.         this.classType = Type.GetType("Hacker");
  31.     }
  32.  
  33.     public string StealFieldInfo(string investigatedClass, params string[] requestedFields)
  34.     {
  35.         this.result.AppendLine($"Class under investigation: {this.classType}");
  36.  
  37.         Object classInstance = Activator.CreateInstance(this.classType);
  38.         foreach (var fieldName in requestedFields)
  39.         {
  40.             FieldInfo fieldInfo = this.classType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  41.             if (fieldInfo != null)
  42.             {
  43.                 this.result.AppendLine($"{fieldInfo.Name} = {fieldInfo.GetValue(classInstance)}");
  44.             }
  45.         }
  46.  
  47.         return result.ToString().Trim();
  48.     }
  49.  
  50.     public string AnalyzeAcessModifiers(string className)
  51.     {
  52.         var fieldsInfo = this.classType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
  53.         foreach (FieldInfo fieldInfo in fieldsInfo)
  54.         {
  55.             if (!fieldInfo.IsPrivate)
  56.             {
  57.                 this.result.AppendLine($"{fieldInfo.Name} must be private!");
  58.             }
  59.         }
  60.  
  61.         var nonPublicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
  62.         foreach (MethodInfo nonPublicMethodInfo in nonPublicMethodsInfo.Where(m => m.Name.StartsWith("get")))
  63.         {
  64.             this.result.AppendLine($"{nonPublicMethodInfo.Name} have to be public!");
  65.         }
  66.  
  67.         var publicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.Public);
  68.         foreach (MethodInfo publicMethodInfo in publicMethodsInfo.Where(m => m.Name.StartsWith("set")))
  69.         {
  70.             this.result.AppendLine($"{publicMethodInfo.Name} have to be private!");
  71.         }
  72.  
  73.         return this.result.ToString().Trim();
  74.     }
  75.  
  76.     public string RevealPrivateMethods(string className)
  77.     {
  78.         this.result.AppendLine($"All Private Methods of Class: {className}");
  79.         this.result.AppendLine($"Base Class: {this.classType.BaseType.Name}");
  80.  
  81.         var nonPublicMethodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
  82.         foreach (MethodInfo methodInfo in nonPublicMethodsInfo)
  83.         {
  84.             this.result.AppendLine(methodInfo.Name);
  85.         }
  86.  
  87.         return this.result.ToString().Trim();
  88.     }
  89.  
  90.     public string CollectGettersAndSetters(string className)
  91.     {
  92.         var methodsInfo = this.classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  93.         foreach (MethodInfo methodInfo in methodsInfo.Where(m => m.Name.StartsWith("get")))
  94.         {
  95.             this.result.AppendLine($"{methodInfo.Name} will return {methodInfo.ReturnType}");
  96.         }
  97.         foreach (MethodInfo methodInfo in methodsInfo.Where(m => m.Name.StartsWith("set")))
  98.         {
  99.             this.result.AppendLine($"{methodInfo.Name} will set field of {methodInfo.GetParameters().First().ParameterType}");
  100.         }
  101.  
  102.         return this.result.ToString().Trim();
  103.     }
  104. }
  105.  
  106. public class Hacker
  107. {
  108.     public string username = "securityGod82";
  109.     private string password = "mySuperSecretPassw0rd";
  110.  
  111.     public string Password
  112.     {
  113.         get => this.password;
  114.         set => this.password = value;
  115.     }
  116.  
  117.     private int Id { get; set; }
  118.  
  119.     public double BankAccountBalance { get; private set; }
  120.  
  121.     public void DownloadAllBankAccountsInTheWorld()
  122.     {
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement