Advertisement
spacechase0

Untitled

May 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3.  
  4. namespace Test
  5. {
  6.     public class A { }
  7.     public class B : A { }
  8.  
  9.     public class Class
  10.     {
  11.         public A a = new A();
  12.     }
  13.  
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             var y = typeof(B);
  19.             var x = typeof(Class).GetField("a");
  20.             var type = x.GetType();
  21.            
  22.             Console.WriteLine(x);
  23.  
  24.             FieldInfo fieldInfo = null;
  25.             for (; type != null && fieldInfo == null; type = type.BaseType)
  26.                 fieldInfo = type.GetField("type", BindingFlags.Instance | BindingFlags.NonPublic);
  27.             fieldInfo.SetValue(x, y);
  28.            
  29.             Console.WriteLine(typeof(Class).GetField("a"));
  30.  
  31.             Console.Read();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement