tags:

views:

139

answers:

4

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

A: 

Use ReadOnly value, if you want a constant value. or just use const.

Use Enum, in case that you have a few 'safe values' that you want to customer (programmer) to choose one of them.

Edit: Apparently, enum not safe enough. you can use readonly field with the following format:

public class SafeValue
{
    private string _value;

    public static readonly SafeValue Value1 = new SafeValue("value1");
    public static readonly SafeValue Value2 = new SafeValue("value2");

    private SafeValue(string value)
    {
        _value = value;
    }

    public override string ToString()
    {
        return _value;
    }
}
Mendy
readonly would work, assuming the value never needs to change once set in the constructor, but how does Enum help? Someone can still change the value through reflection.
Mike Two
@Mike: I edited my answer to explain it.
Mendy
@Mendy - enums are not validated; I can change your enum value to -142526 and the system won't care.
Marc Gravell
@Marc interesting. care to say how?
Mendy
@Mendy - you can cast any int value. The compiler doesn't check. so 'SomeEnum value = -142526;' Won't compile but `SomeEnum value = (SomeEnum) -142526;` will. Even if -142526 isn't a valid value in `SomeEnum`.
Mike Two
See @KMan's point about readonly...
Marc Gravell
@Marc now I'm totally confused. the `readonly` directive compiled into MSIL, but not honored?
Mendy
readonly can't force the field to never change via Reflection (or direct memory tweaking), but it could create all sorts of interesting side effects due to compiler optimizations assuming the field never changes.
Dan Bryant
@Mendy - yup; this is required for remoting and some (not all) forms of serialization
Marc Gravell
+2  A: 

You can even change the private readonly field using reflection. So, your best bet may the const.

KMan
+6  A: 

Please understand that I mean this with all respect:

You are wasting your time. Seriously.

Reflection is not to be considered when specifying an API or contract.

Believe this. It is not possible to stop reflection. If a consumer is bent on peeking into your code and flipping bits that have been hidden for a reason, well, the consequences are on them, not you.

Sky Sanders
+6  A: 

You are trying to protect your system from something that is stronger than your code. If you don't trust certain code, run it with partial trust - then when it asks to reflect your type it will be denied.

Reflection (and related techniques like DynamicMethod) can with enough trust get virtually unrestricted access to your data (even, for example, changing the characters of a string without ever re-assigning it); thus you must reduce trust for non-trusted code.

In a similar way, anyone with debugger or admin access to your process (or the dlls it loads) can subvert your code. This is not an interesting attack, as the "hacker" must already have at least as much access than your code (probably more).

Marc Gravell
The attack is interesting in the case of an anti-theft or anti-tampering package. You can't really protect against somebody crashing your app if they have full control over your app, but you can make it much more difficult to modify certain key state that enforces your protection scheme. I don't think this is what the OP is doing, however.
Dan Bryant