对象集合的对象初始化程序

时间:2009-10-08 17:54:21

标签: c#

我想知道是否有办法初始化List<T>,其中Tobject,就像简单的集合被初始化一样?

Simple Collection Initializer:

List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Object Collection Initilizer:

 List<ChildObject> childObjects = new List<ChildObject>
 {       
        new ChildObject(){ Name = "Sylvester", Age=8 },
        new ChildObject(){ Name = "Whiskers", Age=2 },
        new ChildObject(){ Name = "Sasha", Age=14 }
 };

问题是,如何以及如果你能做这样的事情?

 List<ChildObject> childObjects = new List<ChildObject>
 {       
       { "Sylvester", 8} , {"Whiskers", 2}, {"Sasha", 14}
 };

5 个答案:

答案 0 :(得分:3)

如果你看一下the docs集合初始值设定项,那就是集合的Add方法。只需将封闭的通用List子类化为您的类型,并使用裸参数创建Add方法。像

public class MyColl : List<ChildObject>
{
    public void Add(string s1, int a1, int a2)
    {
        base.Add(new ChildObject(s1, a1, a2));
    }
}

public class ChildObject
{
    public ChildObject(string s1, int a1, int a2)
    {
        //...
    }
}

然后调用它看起来像:

    static void Main(string[] args)
    {
        MyColl x = new MyColl
        {
            {"boo", 2, 4},
            {"mang", 3, 5},
        };
    }

答案 1 :(得分:3)

你可能做的最好的事情就是这样:

public class MyListOfChildObjects : List<ChildObject>
{
    public void Add( string name, int age )
    {
        Add ( new ChildObject { Name = name, Age = age } );
    }
}

var childObjects = new MyListOfChildObjects 
{
    { "Sylvester", 8 } , { "Whiskers", 2 }, { "Sasha", 14 }
};

你可以使它更通用,但你怎么知道应该绑定到每个属性的参数?

public class MyList<T> : List<T>
{
    public void Add( params object[] arguments )
    {
        // what order should I bind the values to?
    }
}

var childObjects = new MyList<ChildObject>
{
    { "Sylvester", 8 } , { "Whiskers", 2 }, { "Sasha", 14 }
};

答案 2 :(得分:3)

您可以通过创建自己的集合来扩展List<ChildObject>并提供自己的添加方法:

public class ChildObjectCollection : List<ChildObject>
{
    public void Add(string name, int age)
    {
        this.Add(new ChildObject(name, age));
    }
}

然后您可以像这样初始化它:

var childObjects = new ChildObjectCollection
{
    { "Sylvester", 8} , {"Whiskers", 2}, {"Sasha", 1 }
};

答案 3 :(得分:1)

根据Lee的回答,如果没有根据List<ChildObject>创建自己的类,则无法执行此操作。遗憾的是,扩展方法不考虑用于收集初始化器,否则这将起作用:

// This doesn't work, but it would if collection initializers checked
// extension methods.
using System;
using System.Collections.Generic;

public class ChildObject
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static class Extensions
{
    public static void Add(this List<ChildObject> children, 
                           string name, int age)
    {
        children.Add(new ChildObject { Name = name, Age = age });
    }
}

class Test
{
    static void Main(string[] args)
    {
        List<ChildObject> children = new List<ChildObject>
        {
            { "Sylvester", 8 },
            { "Whiskers", 2 },
            { "Sasha", 14 }
        };
    }
}

答案 4 :(得分:0)

最接近的是在类上创建一个接受这些参数的参数化构造函数。这不会让你一路走来,但你至少可以这样做:

List<ChildObject> childObjects = new List<ChildObject>
{       
      new ChildObject("Sylvester", 8) , 
      new ChildObject("Whiskers", 2), 
      new ChildObject("Sasha", 14)
};