using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mesnac.Action.ChemicalWeighing.Entity { /// /// PLC设备变量值 /// [Serializable] public class Value { #region 字段定义 private object value = null; #endregion #region 参数构造方法 public Value(object obj) { this.value = obj; } #endregion #region 方法定义 /// /// 转为Object /// /// public object ToObject() { return this.value; } /// /// 转为整型 /// /// 默认值 /// public int ToInt(int defaultValue) { int iResult = defaultValue; if (this.value != null && this.value != DBNull.Value && int.TryParse(this.value.ToString(), out iResult)) { return iResult; } return defaultValue; } /// /// 转为整型 /// /// public int ToInt() { return ToInt(0); } /// /// 转为float类型 /// /// 默认值 /// public float ToFloat(float defaultValue) { float fResult = defaultValue; if (this.value != null && this.value != DBNull.Value && float.TryParse(this.value.ToString(), out fResult)) { return fResult; } return defaultValue; } /// /// 转为float类型 /// /// public float ToFloat() { return ToFloat(0.0f); } /// /// 转为double类型 /// /// 默认值 /// public double ToDouble(double defaultValue) { double iResult = defaultValue; if (this.value != null && this.value != DBNull.Value && double.TryParse(this.value.ToString(), out iResult)) { return iResult; } return defaultValue; } /// /// 转为double类型 /// /// public double ToDouble() { return ToDouble(0.0); } /// /// 转为日期类型 /// /// 默认值 /// public DateTime ToDateTime(DateTime defaultValue) { DateTime iResult = defaultValue; if (this.value != null && this.value != DBNull.Value && DateTime.TryParse(this.value.ToString(), out iResult)) { return iResult; } return defaultValue; } /// /// 转为日期类型 /// /// public DateTime ToDateTime() { return ToDateTime(DateTime.Now); } /// /// 转为字符串 /// /// 默认值 /// public string ToString(string defaultValue) { string iResult = defaultValue; if (this.value != null && this.value != DBNull.Value) { return this.value.ToString(); } return defaultValue; } /// /// 转为字符串 /// /// public override string ToString() { return ToString(string.Empty); } #endregion } }