【问题标题】:Can I use a C# List in this case? [closed]在这种情况下我可以使用 C# 列表吗? [关闭]
【发布时间】:2016-01-19 10:25:15
【问题描述】:

我有一个类似类型的操作列表,我需要为其创建不同的类ActionClass1ActionClass2。 此外,我有不同的站点,我需要在这些站点上执行不同操作的列表。请注意,此列表的内容将取决于站点。

我想开发一个 GUI,以便能够使用 c# 和 WPF 编辑在给定站点 SiteClass 上执行的操作。

我现在的想法是做以下事情:

  1. 从一个通用的抽象操作类 AbstractActionClass 继承不同的操作类 ActionClass1 等。
  2. 创建ActionListAbstractActionClass 列表,其中将包含作为具体实现的不同操作(例如ActionClass1 等)。
  3. 创建SiteClass 实例列表SiteList,其中将包含站点及其相应的操作列表和其他信息(例如站点位置等)。

到目前为止,我对上述内容有一个很好的想法。但是,我现在需要能够为每个站点编辑 ActionList 并在 GUI 中设置样式,以便具有相同 ActionList 的所有站点看起来相同(例如,具有相同的颜色)。因此,我需要能够比较SiteListSiteClass 的每个实例的ActionList,以检查它们是否不同,找出有多少不同的实例,然后在GUI 中相应地设置它们的样式。

所以我的问题是:我能否在SiteClass 中使用ActionList List<AbstractActionClass> 类型的列表来执行诸如“唯一”之类的比较,以找出内部ActionList List<AbstractActionClass> 的唯一出现次数SiteList List<SiteClass> 并将此信息用于样式?有没有更好的办法?

【问题讨论】:

  • MVVM 和日期模板是要走的路。在视图模型中,您将拥有ObservableCollection<T>,其中T 是抽象类或用于访问视图中所需属性/方法的简单接口。 Unique 属性必须由向该集合添加/删除的方法处理。

标签: c# list unique


【解决方案1】:

简短回答:是的

详细回答。您可以创建一个List<AbstractionClass> 并处理不同的渲染,您可以采用两种方法:

  1. 向类添加一个抽象属性,告诉您该类是什么:

    public abstract string ClassType { get; }
    

并在每个不同的动作列表中简单地实现它

public override string ClassType { get { return "ActionClass1";}}

然后在你的渲染代码中做一些类似的事情

switch (ac.ClassType)
{ 
    case "ActionClass1":
        /// render according to class 1;
        break;
    case "ActionClass2":
        /// render according to class 2;
        break;
  }
  1. 你可以做一些简单的转换:

    if (ac is ActionClass1)
    {
        /// render according to class 1
    }
    else if (ac is ActionClass2)
    {
       ///  render according to class 2
    }
    
  2. 您可以抽象列表本身:

    public class ActionClassList : List<ActionClass>
    {
        public abstract string ClassType { get;}
    }
    

并实现 ActionClassList1 和 ActionClassList2 以返回不同的 ClassType。

在接收端,您始终接受一个 ActionClassList,但它可能是您获得的派生类之一。

【讨论】:

    【解决方案2】:

    是的。这可以在 C# 中以多种方式完成,但 C# 列表对此并不坏,因为您熟悉并且可以更好地理解这个想法,所以我不会说使用 C# 列表有什么错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 2016-09-12
      • 2016-11-07
      • 2022-08-05
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多