创建和使用自定义列表< T>在C#中 [英] Creating and using a custom List<T> in C#

查看:118
本文介绍了创建和使用自定义列表< T>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个自定义列表中我添加了一些额外的工具。我想这个名单适用于我创建自定义类的一个长长的清单。所有的类都有一个ID号,以及一些在列表中使用的ID的工具。



下面是我尝试使用我的代码部分。我希望这有助于你理解我的问题。



 命名空间Simple_Point_of _Sales_System 
{
公共类MYLIST< T> ; :列表< T>
{
内部INT SETID()
{
返回this.Max(N => n.ID)+ 1;
}
内部找不到(INT ID)
{
返回this.Find(N => n.ID == ID);
}
内部不要再增加(T N)
{
阅读();
添加(N);
写();
返回N;
}
内部无效删除(INT ID)
{
阅读();
如果(this.Exists(T => t.ID == ID))removeall过(T => t.ID == ID);
,否则MessageBox.Show(的GetType()名++身份证+不存在,丢失物品,MessageBoxButtons.OK,MessageBoxIcon.Error);
写();
}
内部空隙编辑(INT ID,T N)
{
阅读();
如果(this.Exists(T => t.ID == ID))该[FindIndex(T => t.ID == ID)] = N;
,否则MessageBox.Show(的GetType()名++身份证+不存在,丢失物品,MessageBoxButtons.OK,MessageBoxIcon.Error);
写();
}
内部MYLIST< T>阅读()
{
清除();
StreamReader的SR =新的StreamReader(@../../文件/+的GetType()名+名.txt); (!sr.EndOfStream)
,而
加入(新T()套装(sr.ReadLine()));
sr.Close();
返回这一点;
}
内部空隙写()
{
的StreamWriter SW =新的StreamWriter(@../../文件/+的GetType()。名称+.TXT );
的foreach(T N在这)
sw.WriteLine(n.ToString());
sw.Close();
}
}

公共类客户
{
公众诠释身份证;
公共字符串名字;
公共字符串姓氏;
}

公共类项目
{
公众诠释ID {搞定;组; }
公共字符串等级{搞定;组; }
公共字符串名称{;组; }
公共双价{搞定;组; }
}

公共MyClass类
{
MYLIST<客户>客户=新MYLIST<客户>();
MYLIST<项目>项目=新MYLIST<项目>();
}
}


解决方案

我想你的自定义列表需要把一些约束的泛型类型,以允许。我会更新你的签名,以类似

 公共类MYLIST< T> :列表< T>其中T:{IIdentity的....} 

下面我用的IIdentity 作为接口定义 ID ,但它也可能是一个类。



要更新你的代码我会做这样的事情:

 公共接口的IIdentity 
{
INT ID {搞定; }
}

公共类客户:IIdentity的
{
公众诠释ID {搞定;设置;}
公共字符串名字;
公共字符串姓氏;
}

公共类项目:IIdentity的
{
公众诠释ID {搞定;组; }
公共字符串等级{搞定;组; }
公共字符串名称{;组; }
公共双价{搞定;组; }
}



我在客户改变了ID 是一个属性,而不是实例变量。


I am trying to use a customized List were I have added a few additional tools. I want to apply this list to a long list of customized classes that I have created. All of the classes have an ID number and some of the tools in the List use the ID.

Here is a portion of my code that I am trying to use. I hope this help you understand my problem.

namespace Simple_Point_of _Sales_System
{
    public class MyList<T> : List<T>
    {
        internal int SetID()
        {
            return this.Max(n => n.ID) + 1;
        }
        internal T Find(int ID)
        {
            return this.Find(n => n.ID == ID);
        }
        internal T Add(T n)
        {
            Read();
            Add(n);
            Write();
            return n;
        }
        internal void Remove(int ID)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) RemoveAll(t => t.ID == ID);
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal void Edit(int ID, T n)
        {
            Read();
            if (this.Exists(t => t.ID == ID)) this[FindIndex(t => t.ID == ID)] = n;
            else MessageBox.Show(GetType().Name + " " + ID + " does not exist.", "Missing Item", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Write();
        }
        internal MyList<T> Read()
        {
            Clear();
            StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
            while (!sr.EndOfStream)
                Add(new T().Set(sr.ReadLine()));
            sr.Close();
            return this;
        }
        internal void Write()
        {
            StreamWriter sw = new StreamWriter(@"../../Files/" + GetType().Name + ".txt");
            foreach (T n in this)
                sw.WriteLine(n.ToString());
            sw.Close();
        }
    }

    public class Customer
    {
        public int ID;
        public string FirstName;
        public string LastName;
    }

    public class Item
    {
        public int ID { get; set; }
        public string Category { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }

    public class MyClass
    {
        MyList<Customer> Customers = new MyList<Customer>();
        MyList<Item> Items = new MyList<Item>();
    }
}

解决方案

I think your custom list needs to put on some constraints on the generic type to allow that. I would update your signature to something like

public class MyList<T> : List<T> where T : IIdentity { .... }

Here I used IIdentity as the interface defining ID, but it could also be a class.

To update your code I would do something like this:

public interface IIdentity
{
    int ID { get; }
}

public class Customer : IIdentity
{
    public int ID { get; set;}
    public string FirstName;
    public string LastName;
}

public class Item : IIdentity
{
    public int ID { get; set; }
    public string Category { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

I changed the ID in Customer to be a property instead of instance variable.

这篇关于创建和使用自定义列表&LT; T&GT;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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