Protect value from changies using reflection?
- by IordanTanev
Hi,
here is the problem case i am writing a little third party library. In this library i have a class like this
public class TestClass
{
public int TestField { get; private set; }
public TestClass( )
{
TestField = 1;
}
}
Then i have a varialbe form this class like this
public TestClass test = new TestClass( );
The problem i am facing is that usnig reflection like this
PropertyInfo field = typeof( TestClass ).GetProperty( "TestField" );
field.SetValue( test, 2, null );
programers can change internal value of this class. this will be very bad thing becouse it can crash the hole library. My question is what is the best way to protect my code form such changes.I know i can use some kind of bool flag so tha value can be changed only ones but this is not very good salution is there a better one?
Best Regards,
Iordan