简体   繁体   中英

overload operator = in C#. How can i accept other types?

So a friend was telling me how a game was hacked and how the technique worked. He then asked whats the best way to prevent that kind of attack. The most straight forward way i knew was to A) the shuffle the bits of important value B) hash the values and compare them every time (an int that holds the score or money is likely to be checked rarely).

Then i tried the implementation in C#, i couldnt overload the = operator. How can i do this?

ex code.

class EncryptVal <T>
{
    T v;
    public T operator = (T v2)
    {
        //shuffle bits
    }
    public T operator ()()
    {
        //return unshuffle bits
    }
}

You're looking for the implicit and explicit operator, rather than saying = . This allows you to define how things will work when cast implicitly (ie, just an assignment) and explicitly (ie, there's a casting operator).

public static implicit operator Type1(Type2 p) {}
public static explicit operator Type1(Type2 p) {}

You can encapsulate the value in the class and overload the implicit conversions to and from the class:

public class EncryptVal<T> {

    private T _value;

    private EncryptVal(T value) {
        _value = value;
    }

    public static implicit operator EncryptVal<T>(T value) {
        //shuffle bits
        return new EncryptVal<T>(value);
    }

    public static implicit operator T(EncryptVal<T> value) {
        //unshuffle bits
        return value._value;
    }

}

Usage:

// implicit conversion from int
EncryptVal<int> e = 42;

// implicit conversion to int
int i = e;

You are not allowed to overload the assignment operator in C#. Here's the MSDN documentation on it .

You'll have to create your own function for this behavior.

I assume that you come from C++ where it is very common to write classes that are used like primitive data types. In C# you do things more explicitly.

I would write it as a property or as two methods, eg:

class EncryptVal <T>
{
    T v;
    public T Value
    {
      get
      {
        //return unshuffle bits
      }
      set
      {
        //shuffle bits
      }
    }
}

Dont use = for setting the value. You cant overload assignment.

What you can do is hide it behind a property.

int _encyptedValue;
Public int myInt
{
    get
    {
        return Decrypt(_encryptedValue);
    }
    set
    {
         _encryptedValue = Encrypt(value);
    }
}

You get to chosse your decryption/encryption

I would go the for implicit/explicit operator overloading for the implementation part.

Probably the explicit one since your conversion does heavy processing, and that it eventually could fail.

I would just add that shuffling bits seems to be only an obfuscation technic that will surely not last long if you have wishfull hackers interested in your game. You probably need stronger cryptography to protect your data, but more context is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM