c# - 不重新定义类型的隐式运算符重载
问题描述
我需要重载隐式运算符,但我需要修改自己的值,而不是为类型创建新值。
public abstract class AbstractScriptableValue<T>//this generic abstract class
{
[SerializeField] protected T value;//internal value
protected T previusValue;//previus internal value, for know if the value changes
public T Value
{
get { return value; }
set
{
this.value = value;
if (!this.value.Equals(previusValue))//if the value change
{
previusValue = this.value;//update
OnValueChanged();//and very importantan!!! notify for a change
}
}
}
public Action OnValueChanged;
}
public class ValueInt : AbstractScriptableValue<int>
{
public static implicit operator ValueInt(int argValueA)
{
this.Value = argValueA;//error here, because i need evaluate the previus value for notify if it changes, then i set the value for the property .Value, if in this point i create a new value, then i cant notify if it changes.
return this;
}
}
感谢您帮助我搜索允许我通知值更改的解决方案。
解决方案
implicit
运算符重载方法是static
,因此没有可以访问的实例。相反,创建您的类的新实例,然后访问该属性:
public static implicit operator ValueInt(int argValueA)
{
var result = new ValueInt(); // or whatever constructor you want to call
result.Value = argValueA;
return result;
}
推荐阅读
- python - Python - NULL data causing keyerror on DB INSERT
- python - Django Makemigrations 和 Migrate 不断重复
- c# - 实时数据库错误检索 - Unity3D
- c# - 使用 QuickFIX/n 接收每个报价/价格变动的买入/卖出价格的最佳方式是什么
- android - Android - 方向更改时更改按钮文本大小
- node.js - 错误:参数必须是 Aggregate.append 处的聚合管道运算符
- python - 是否有熊猫功能以设定的时间间隔重复行?
- angular - 已处理字符串中的角度路由器斜线会破坏 url 匹配
- laravel - 从值加入 Laravel 在视图刀片中显示数据
- c# - Smart.Format 版本 2.5.0 升级后出错