Saturday, 3 December 2016

Accessing a private static C# variable

Variables in BOLDALLCAPS are things you put in yourself. 

First make sure you have:
using System.Reflection;

Getting access to the field:
var fieldInfo = typeof(CLASSNAME).GetField("FIELDNAME", BindingFlags.NonPublic | BindingFlags.Static);

Getting the field value:
FIELDTYPE myVariable = (FIELDTYPE)fieldInfo;

Setting the field value:
fieldInfo.SetValue(null, myVariable);

No comments:

 
Register your domains here!