为了账号安全,请及时绑定邮箱和手机立即绑定

从通用对象列表中读取通用值

从通用对象列表中读取通用值

C#
catspeake 2021-06-04 11:18:51
我正在尝试遍历通用对象调用列表Condition<T>以读取通用字段Value。我跟着这个问题能够存储List<Condition<T>>. 我现在遇到的问题是我无法Value在循环中使用该字段。我需要更改什么才能使用该Value字段?主要的string url = "";List<ConditionBase> Conditions = new List<ConditionBase>();Conditions.Add(new Condition<int>(Field.Field1, 1, ConditionOperator.Equal))Conditions.Add(new Condition<string>(Field.Field2, "test", ConditionOperator.NotEqual))foreach (ConditionBase c in Conditions){    if (c.GetType() == typeof(string))    {        // c.Value throws an error        url += c.Field + " " + c.ConditionOperator + " '" + c.Value + "' and ";    }    else if (c.GetType() == typeof(DateTime))    {        // c.Value throws an error        url += c.Field + " " + c.ConditionOperator + " " + Helpers.FormatDate(c.Value) + " and ";    }}条件基础public interface ConditionBase{    Field Field { get; set; }    ConditionOperator ConditionOperator { get; set; }}状况public class Condition<T> : ConditionBase{    private Field _Field;    private T _Value;    private ConditionOperator _ConditionOperator;    public Condition(Field field, T value, ConditionOperator condition)    {        this._Field = field;        this._Value = value;        this._ConditionOperator = condition;    }    public Field Field    {        get        {            return this._Field;        }        set        {            if (this._Field != value)            {                this._Field = value;            }        }    }    public T Value    {        get        {            return this._Value;        }        set        {            if (!EqualityComparer<T>.Default.Equals(this._Value, value))            {                this._Value = value;            }        }    }    public ConditionOperator ConditionOperator    {        get        {            return this._ConditionOperator;        }        set        {            if (this._ConditionOperator != value)            {                this._ConditionOperator = value;            }        }    }}
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信