Tag: generic list

C#更新绑定到通用列表的combobox

我的表单上有一个combobox,它绑定到一个通用的字符串列表,如下所示: private List mAllianceList = new List(); private void FillAllianceList() { // Add alliance name to member alliance list foreach (Village alliance in alliances) { mAllianceList.Add(alliance.AllianceName); } // Bind alliance combobox to alliance list this.cboAlliances.DataSource = mAllianceList; } 然后,用户可以在combobox中添加或移除项目。 我在别处读过,只需在通用列表中添加或删除项目,combobox的内容就会自动更新; 如果我对它使用Sort(),应该会发生同样的事情。 但由于某种原因,我无法做到这一点。 我可以看到combobox的DataSource属性在我添加/删除/排序项时正确更新,但combobox中显示的内容不是DataSource属性中的内容。 我肯定错过了什么或做错了什么。 提前致谢! 编辑: 我选择的答案解决了添加和删除的问题,但是BindingList对象无法排序,这对我来说是必要的。 我找到了一个解决方案,通过inheritanceBindingList和添加排序function来构建自定义类,但我想知道在我的情况下是否有更简单的解决方案。 关于如何轻松解决这个问题的任何建议?

C#将类暴露给COM – 通用集合

我们有一个用C#.Net 2.0编写的小框架,我们想要向COM公开。 问题是,我们有一些通用类,将公开如下: interface IOurClass { ReadonlyCollection OurCollection { get; } } interface IOurListObject { //Some properties that don’t matter } 将通用集合暴露给COM的最佳(或推荐方法)是什么? 我们不必支持generics,我们只需要以某种方式公开IOurListObject的集合。 我们也希望避免为我们使用的每个集合编写新类,但可能无法实现。

如何将DataTable转换为C#中的对象类型List

我想在C#中将DataTable转换为List 。 这是我的代码。 但它没有用。 请帮我 public List ShowMessage() { List obj = new List(); DataTable dt = new DataTable(); dt.Columns.Add(“ID”); dt.Columns.Add(“Name”); dt.Rows.Add(“1″,”AAA”); dt.Rows.Add(“2”, “BBB”); dt.Rows.Add(“3”, “CCC”); foreach (DataRow dr in dt.Rows) { obj.Add(dr); } return obj; } 这是错误 –

C#将通用列表序列化为文件

我有一个类,其中包含有关图片的信息,如filepath,hashvalue,bytes。 在另一个类中,我得到了一个通用列表,其中我放置了包含图片信息的类中的对象。 该类看起来像这样: [Serializable()] class PicInfo : ISerializable { public string fileName { get; set; } public string completeFileName { get; set; } public string filePath { get; set; } public byte[] hashValue { get; set; } public PicInfo() { } public PicInfo(SerializationInfo info, StreamingContext ctxt) { this.fileName = (string)info.GetValue(“fileName”, typeof(string)); this.completeFileName = (string)info.GetValue(“completeFileName”, typeof(string)); […]

使用reflection的通用列表

我有一个场景,我将Class name as a string我只想创建该类的通用List 。 我尝试了以下内容 Type _type = Type.GetType(className); List list = new List(); 但是我觉得error 。 我不能把那个_type作为列表参数。 如果有人知道请帮助我

将基类型列表转换为inheritance类型列表

我确信这个问题可以解决上一个问题中提出的问题,但我无法找到它。 C#类中有一个方法,它将基类的通用List作为参数。 我需要传递一个inheritance类的列表,并且不确切知道如何执行此操作。 我的尝试中出现错误。 以下是示例代码: public class A { public static void MethodC(List) { // Do Something here with the list } } public Class B : A { // B inherits from A, A is the Base Class } // Code utilizing the above method List listOfB = new List(); A.MethodC( (List) listOfB ); […]

如何在不使用foreach的情况下将ArrayList转换为强类型通用列表?

请参阅下面的代码示例。 我需要ArrayList作为通用List。 我不想使用foreach 。 ArrayList arrayList = GetArrayListOfInts(); List intList = new List(); //Can this foreach be condensed into one line? foreach (int number in arrayList) { intList.Add(number); } return intList;

无法更改generics集合中struct的成员值

想象一下这个struct : struct Person { public string FirstName { get; set; } public string LastName { get; set; } } 以下代码: var list = new List(); list.Add(new Person { FirstName = “F1”, LastName = “L1” }); list.Add(new Person { FirstName = “F2”, LastName = “L2” }); list.Add(new Person { FirstName = “F3”, LastName = “L3” […]

List 的XML序列化 – XML Root

关于Stackoverflow的第一个问题(.Net 2.0): 所以我试图返回一个List的XML,其中包含以下内容: public XmlDocument GetEntityXml() { StringWriter stringWriter = new StringWriter(); XmlDocument xmlDoc = new XmlDocument(); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); XmlSerializer serializer = new XmlSerializer(typeof(List)); List parameters = GetAll(); serializer.Serialize(xmlWriter, parameters); string xmlResult = stringWriter.ToString(); xmlDoc.LoadXml(xmlResult); return xmlDoc; } 现在,这将用于我已经定义的多个实体。 说我想获得List的XML XML将类似于: Tom 2 Bob 3 获取这些实体时,有没有办法让我一直得到相同的Root? 例: Tom 2 Bob 3 另请注意,我不打算将XML反序列化回List

通用对象的通用列表

假设我有一个表示数据字段的对象,该对象需要以下属性:Name,Type,Value,Length。 这是对象: class Field { public string Name { get; set; } public Type Type { get { return typeof(T); } } public int Length { get; set; } public T Value { get; set; } } 我使用了generics,因为我想强制代码的用户只能分配某种类型的值。 现在的问题是我想创建一个字段列表。 如果我创建List<Field>类的List<Field>那么我们可以将任何Value分配给列表中的给定Field,当我们查询Type时,我们得到’object’。 事情是 – 在那个列表上我可能想要几个字段持有字符串,很少持有整数,日期,甚至自定义对象反过来将有一个字段列表… generics是一个很好的解决方案吗? 如果是,我将如何实施呢? 如果没有,有什么更好的方法? – -编辑 – – 只是为了增加一些背景: 1.我可能想要一个字段列表,每个字段将保存不同的数据类型,如下所示: List<Field> lst = […]