Skip to content

Instantly share code, notes, and snippets.

@thallippoli
Created November 14, 2012 06:49
Show Gist options
  • Save thallippoli/4070704 to your computer and use it in GitHub Desktop.
Save thallippoli/4070704 to your computer and use it in GitHub Desktop.
Initializing private members using reflection
public void Initialize()
{
FieldInfo[] fields = this.GetType().GetFields( BindingFlags.NonPublic | BindingFlags.Instance );
for( int i = 0; i < fields.Length; i++ )
{
if( fields[ i ].Name == "m_baz" )
fields[ i ].SetValue( this, 42 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment