Protect value from changies using reflection?
Posted
by IordanTanev
on Stack Overflow
See other posts from Stack Overflow
or by IordanTanev
Published on 2010-04-25T06:35:22Z
Indexed on
2010/04/25
6:43 UTC
Read the original article
Hit count: 187
c#
|reflection
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
© Stack Overflow or respective owner