多态,泛型和匿名类型C# [英] polymorphism, generics and anonymous types C#

查看:102
本文介绍了多态,泛型和匿名类型C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Document - > Section - > Body - > Items



Document has部分,一个部分包含一个主体。一个正文有一些文本和一个项目列表。这些项目就是问题所在。有时项目是一个基本的字符串列表,但有时这些项目包含一个自定义数据类型的列表。



因此:

  public class Document 
{
public Section [] Section {get; set;}
}

public class Section
{
public SectionType {get; set;}
public Body {get; set;}
}

public class Body
{
//我希望项目取决于节类型。
//如果例如sectiontype是体验,我希望用类型// Experience创建项目。如果sectiontype是默认值,我希望使用类型字符串
创建项目public Items< T> Items {get; set;}
}

public class Items< T>:IEnumerable,IEnumerator
{
//创建一个可枚举集合的所有管道


public class Experience
{
public string Prop1 {get; set;}
public string Prop2 {get; set;}
}

我无法得到这个工作。属性Items必须由一个类型定义才能编译。我被困在这里。我可以通过为每一种我使用的节创建一个Section类来解决这个问题。但问题是所有其他代码都是相同的,并且该部分上的所有操作都是相同的。唯一不同的是Body中使用的列表类型。



这是最佳做法。我已经尝试过泛型,抽象等。如果直接从调用程序创建Items类,我可以使其工作,但如果Items被声明为另一个类的属性,则无法使其工作。



如果需要,我可以提供更多细节。感谢大家和女生的支持。

解决方案

为物品制作界面

  public interface IItems:IEnumerable,IEnumerator {
}

public class Items< T>:IItems
{
//创建一个可枚举集合的所有管道
}

然后使用那个无处不在的

  public class Body 
{
//我希望项目取决于节类型。
//如果例如sectiontype是体验,我希望用类型// Experience创建项目。如果sectiontype是默认值,我希望使用类型字符串
public IItems Items {get; set;}
}

创建项目。

Consider the following scenario.

Document -> Section -> Body -> Items

Document has sections, a section contains a body. A body has some text and a list of items. The items is what the question is about. Sometimes the items is a basic list of string, but sometimes the items contain a list of a custom datatype.

So:

    public class Document
    {
        public Section[] Sections{get;set;}
    }

    public class Section
    {
         public SectionType Type{get;set;}
         public Body {get;set;}
    }

    public class Body
    {
      //I want the items to be depending on the section type.
      //If e.g. the sectiontype is experience, I want the Items to be created with type //Experience. If sectiontype is default I want the Items to be created with type string
       public Items<T> Items {get;set;}
    }

   public class Items<T>:IEnumerable, IEnumerator
   {
    // Do all the plumbing for creating an enumerable collection
    }

   public class Experience
   {
      public string Prop1{get;set;}
      public string Prop2 {get;set;}
   }

I can´t get this to work. The property Items has to be defined by a type in order to compile. I am stuck here. I can fix this easily by creating a Section class for each of the kind of sections I use. But the thing is that all the other code is the same and all the operations on the section will be the same. The only thing different is the type of list used in the Body.

What is the best practice for this. I have tried generics, abstraction etc. I can make it work if creating the Items class directly from the calling program, but I can´t get it to work if Items is declared a property on another class.

I can provide more details if needed. Thank you guys and girls for your support.

解决方案

Make an interface for Items

   public interface IItems: IEnumerable, IEnumerator{
   }

   public class Items<T>: IItems
   {
    // Do all the plumbing for creating an enumerable collection
    }

Then use that everywhere else.

public class Body
{
  //I want the items to be depending on the section type.
  //If e.g. the sectiontype is experience, I want the Items to be created with type //Experience. If sectiontype is default I want the Items to be created with type string
   public IItems Items {get;set;}
}

这篇关于多态,泛型和匿名类型C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆