Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Foo
- {
- private readonly int bar;
- public Foo(int num)
- {
- bar = num;
- }
- public int GetBar()
- {
- return bar;
- }
- }
- Foo foo = new Foo(123);
- Console.WriteLine(foo.GetBar()); // display 123
- // reflection code here...
- Console.WriteLine(foo.GetBar()); // display 456
- typeof(Foo)
- .GetField("bar",BindingFlags.Instance|BindingFlags.NonPublic)
- .SetValue(foo,567);
- using System;
- using System.Reflection;
- public class Test
- {
- private readonly string foo = "Foo";
- public static void Main()
- {
- Test test = new Test();
- FieldInfo field = typeof(Test).GetField
- ("foo", BindingFlags.Instance | BindingFlags.NonPublic);
- field.SetValue(test, "Hello");
- Console.WriteLine(test.foo);
- }
- }
- SoundDef mySound = Reflection_Modified_Readonly_SoundDef_Field;
- if( !(mySound is SoundDef) )
- Log("Welcome to impossible-land!"); //This would run
- using System;
- namespace TestReadOnly
- {
- class Program
- {
- private readonly int i;
- public Program()
- {
- i = 66;
- }
- private unsafe void ForceSet()
- {
- fixed (int* ptr = &i) *ptr = 123;
- }
- static void Main(string[] args)
- {
- var program = new Program();
- Console.WriteLine("Contructed Value: " + program.i);
- program.ForceSet();
- Console.WriteLine("Forced Value: " + program.i);
- }
- }
- }
Add Comment
Please, Sign In to add comment