简体   繁体   English

在C#中具有通用列表的类

[英]Class having Generic List in C#

I have a class like: 我有一个类:

 public class boolMessage
{
    public bool Message { get; set; }
    public ResponseType Resp { get; set; }

    public boolMessage(bool _Message, ResponseType _Resp)
    {
        Message = _Message;
        Resp = _Resp;
    }
}

What i want to do is to create a class something like: 我想要做的是创建一个类,如:

 public class listMessage
{
    public List<T> Message { get; set; }
    public ResponseType Resp { get; set; }

    public listMessage(List<T> _Message, ResponseType _Resp)
    {
        Message = _Message;
        Resp = _Resp;
    }
}

I think i can do with something like Generic Classes. 我想我可以做像Generic Classes这样的事情。 I have googled it open lot of pages but not able to find some easy solution. 我用Google搜索了很多页面,但却找不到一些简单的解决方案。

Why i required is like i want to return a array of object that are of different types like Student[] or Teacher[]. 为什么我需要就像我想要返回一个不同类型的对象数组,如Student []或Teacher []。

These classes like Teacher are generated by Entity Framework. 像Teacher这样的类是由Entity Framework生成的。

I would like to return some additional information with them ... 我想向他们返回一些额外的信息......

Any help is appreciated 任何帮助表示赞赏

  class ListMessage<T> {
     ...
  }

Further comments: 进一步评论:

  1. The name of your constructor in ListMessage is wrong. ListMessage中构造函数的名称是错误的。
  2. You should not expose member variables to the outside. 您不应该将成员变量暴露给外部。

With regard to 2., I would do the following: 关于2.,我会做以下事情:

class ListMessage<T> {
   private List<T> _messages;
   public IEnumerable<T> Messages { get { return _messages; } }

   public ListMessage (IEnumerable<T> messages) {
      this._messages = messages.ToList ();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM