简体   繁体   English

重载operator =在C#中。 我怎么能接受其他类型?

[英]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). 我知道的最直接的方式是A)洗牌重要值的比特B)散列值并每次比较它们(保持得分或金钱的int很可能很少被检查)。

Then i tried the implementation in C#, i couldnt overload the = operator. 然后我尝试在C#中实现,我无法重载=运算符。 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 = . 你正在寻找implicitexplicit运算符,而不是说= 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#. 不允许在C#中重载赋值运算符。 Here's the MSDN documentation on it . 这是关于它的MSDN文档

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. 我假设您来自C ++,其中编写像原始数据类型一样使用的类是很常见的。 In C# you do things more explicitly. 在C#中,您可以更明确地执行操作。

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. 您可能需要更强大的加密来保护您的数据,但需要更多的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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