0

I have tried many ways like Cast<CustomObject>, as Customobject and ToArray(Customobject) but nothing worked.

How can I add List or ArrayList via AddRange to a CustomObject[] Array?

Code is really difficult. But if you have some time you can get the complete source of the destination list from here: http://www.codeproject.com/Articles/4012/C-List-View-v1-3?msg=3844172#xx3844172xx

This is a Custom Listview I activated a combobox for the second column, so I can select diferrent values for a cell. But before this, I have to add something to select. This is the hole problem.

Update: Firstly, thanks for the help ! Secondly, Found a solution in the comments from the website with the source. Had to add some code and changed the destination custom array to a List


Test cases are associated to test sets via the TestCases collection on TestSet in the WSAPI. This code example demonstrates creating a few test cases and then creating a test set associated with the newly created test cases.

RallyRestApi restApi = new RallyRestApi(new URI(SERVER),
    USERNAME, PASSWORD);

try {

    //Create some test cases
    JsonArray testCases = new JsonArray();
    for(int i = 0; i < 3; i++) {
        JsonObject newTestCase = new JsonObject();
        newTestCase.addProperty("Name", "New Test Case " + i);
        CreateRequest createRequest = new CreateRequest("testcase", newTestCase);
        CreateResponse createResponse = restApi.create(createRequest);
        String ref = createResponse.getObject().get("_ref").getAsString();
        System.out.println(String.format("Created test case %s", ref));

        //Keep track of the test case
        JsonObject testCase = new JsonObject();
        testCase.addProperty("_ref", ref);
        testCases.add(testCase);
    }

    //Create test set
    JsonObject newTestSet = new JsonObject();
    newTestSet.addProperty("Name", "New Test Set");
    newTestSet.add("TestCases", testCases);
    CreateRequest createRequest = new CreateRequest("testset", newTestSet);
    CreateResponse createResponse = restApi.create(createRequest);
    System.out.println(String.format("Created test set %s", createResponse.getObject().get("_ref").getAsString()));

} finally {
    //Release all resources
    restApi.close();
}
4

5 回答 5

6
list.Cast<CustomObject>().ToArray()

只要列表中的内容实际上是CustomObject. 如果它们可能是其他类型,您可以使用OfType<CustomObject>()代替Cast. 这将过滤掉任何不兼容的类型。

于 2012-09-13T12:33:17.723 回答
1

假设objects确实是 的实例CustomObject,请使用 LINQSelect方法:

objList.Select(o => o as CustomObject).ToArray();

否则你会得到一个数组null

于 2012-09-13T12:32:39.603 回答
0

如果它是一个List<CustomObject>那么让我们说

CustomObject[] coarr = list_of_customobject.ToArray();

如果它是一个 ArrayList 那么

CustomObject[] coarr = arraylist.OfType<CustomObject>().ToArray();   
于 2012-09-13T12:33:08.720 回答
0

如果您不确定您的所有对象是否都属于该类型,请CustomObject 尝试

var result = list.OfType<CustomObject>.ToArray();
于 2012-09-13T12:34:53.807 回答
0

严格来说,您不能元素添加到数组中,因为数组的长度在其生命周期内保持不变。你可以做两件事:

创建一个新数组

myArray = myTList.ToArray() // generic)
myArray = myArrayList.Cast<CustomObject>().ToArray() // cast, non-generic
myArray = myArrayList.OfType<CustomObject>().ToArray() // filter by type, non-generic

设置数组的元素

myArray[x] = myTList[y] // generic
myArray[x] = (CustomObject)myArrayList[y] // non-generic

我建议您尽可能使用通用集合。它们为您提供额外的类型安全性。强制转换object变量会导致运行时错误,您可以使用泛型类型在编译时检测到。

如果您确实想向现有集合添加元素,您可以尝试使用动态集合类型而不是数组:List<T> : IList<T>orLinkedList<T> : ICollection<T>是一个很好的起点,或者更具体的类型,例如Stack<T>or Queue<T>

于 2012-09-13T12:48:58.507 回答