Guest User

Untitled

a guest
Feb 21st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public class Foo
  2. {
  3. private readonly int bar;
  4.  
  5. public Foo(int num)
  6. {
  7. bar = num;
  8. }
  9.  
  10. public int GetBar()
  11. {
  12. return bar;
  13. }
  14. }
  15.  
  16. Foo foo = new Foo(123);
  17. Console.WriteLine(foo.GetBar()); // display 123
  18. // reflection code here...
  19. Console.WriteLine(foo.GetBar()); // display 456
  20.  
  21. typeof(Foo)
  22. .GetField("bar",BindingFlags.Instance|BindingFlags.NonPublic)
  23. .SetValue(foo,567);
  24.  
  25. using System;
  26. using System.Reflection;
  27.  
  28. public class Test
  29. {
  30. private readonly string foo = "Foo";
  31.  
  32. public static void Main()
  33. {
  34. Test test = new Test();
  35. FieldInfo field = typeof(Test).GetField
  36. ("foo", BindingFlags.Instance | BindingFlags.NonPublic);
  37. field.SetValue(test, "Hello");
  38. Console.WriteLine(test.foo);
  39. }
  40. }
  41.  
  42. SoundDef mySound = Reflection_Modified_Readonly_SoundDef_Field;
  43. if( !(mySound is SoundDef) )
  44. Log("Welcome to impossible-land!"); //This would run
  45.  
  46. using System;
  47.  
  48. namespace TestReadOnly
  49. {
  50. class Program
  51. {
  52. private readonly int i;
  53.  
  54. public Program()
  55. {
  56. i = 66;
  57. }
  58.  
  59. private unsafe void ForceSet()
  60. {
  61. fixed (int* ptr = &i) *ptr = 123;
  62. }
  63.  
  64. static void Main(string[] args)
  65. {
  66. var program = new Program();
  67. Console.WriteLine("Contructed Value: " + program.i);
  68. program.ForceSet();
  69. Console.WriteLine("Forced Value: " + program.i);
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment