4

I found something similar on another question but i couldnt solve my problem as i'm still quite new in c#.

i have

System.Type type; // for example int[]
System.Type elementType = type.GetElementType(); //for example int

and a List that i need to cast to int[]

object valueObj; //is my List<object> i want to cast to int[]

I'm trying the following:

MethodInfo castMethod = typeof(Enumerable).GetMethod("Cast")
    .MakeGenericMethod( new System.Type[]{ elementType } );
MethodInfo toArrayMethod = typeof(Enumerable).GetMethod("ToArray")
     .MakeGenericMethod( new System.Type[]{ elementType } );

var castedObjectEnum = castMethod.Invoke(null, new object[] { valueObj });
var castedObject = toArrayMethod.Invoke(null, new object[] { castedObjectEnum });

in the last line i get this runtime exception

InvalidCastException: Cannot cast from source type to destination type. System.Linq.Enumerable+c__Iterator01[System.Int32].MoveNext () System.Collections.Generic.List1[System.Int32].AddEnumerable (IEnumerable`1 enumerable)

as a last thing i need to assign the casteObject to a privateField using

field.SetValue(this, castedObject);

Edit: I will try and get more info on that but to better explain what i'm trying to do i will explain the use case.

i serialize to disk in json some class fields, and then when i deserialize i get back a List where i serialize a base array of int/bools/string

I'm doing the serialization and deserialization by marking fields with a custom attribute, and i want to be able to serialize both Array and Lists of any basic type

i will double check and get with more info


jQuery .toggle() + addClass() doesn't seem to have desired effect

I am trying to create a link <a> with two different backgrounds, one a "Down Arrow" and one an "Up Arrow" depending on the classname arrow-up and arrow-down.

So when you click on the down arrow, the div named product-add-wrapper slides down, and the down arrow* becomes an **up arrow and vice versa.

The problem is, the .toggle + callback seems to work fine, it adds the desired class name and removes the desired class name, however, the background-image doesn't change (the down arrow doesn't become an up arrow).

Here is the html.

<span class="arrow-right">
    <a class="updown arrow-down">&nbsp;</a>
</span>

Here is the css.

.updown {
    display: block;
    margin-top: -1px;
    height: 25px;
    width: 25px;
}

.arrow-up {
    background-image: url('../img/arrow-up.png');
}

.arrow-down {
    background-image: url('../img/arrow-down.png');
}

And here is the javascript.

$('.updown').click(function() {
    $('#product-add-wrapper').toggle('slow', function() {
        var classname = $('.updown').attr('class');
        if (classname === 'up arrow-down') {
            $('.updown').removeClass('arrow-down').addClass('arrow-up');
        } else {
            $('.updown').removeClass('arrow-up').addClass('arrow-down');
        }
    });
});

Any ideas? Thanks in advance.

4

2 回答 2

4

我不知道你做错了什么。

这是一个演示的LINQPad程序,它似乎使用与您相同的调用,但我确实得到了正确类型数组的输出。

void Main()
{
    object valueObj = new List<object> { 1, 2, 3 };
    Type type = typeof(int[]);

    Type elementType = type.GetElementType();
    MethodInfo castMethod = typeof(Enumerable).GetMethod("Cast")
        .MakeGenericMethod( new System.Type[]{ elementType } );
    MethodInfo toArrayMethod = typeof(Enumerable).GetMethod("ToArray")
        .MakeGenericMethod( new System.Type[]{ elementType } );

    var castedObjectEnum = castMethod.Invoke(null, new object[] { valueObj });
    var castedObject = toArrayMethod.Invoke(null, new object[] { castedObjectEnum });
    castedObject.Dump();
}

确实得到了一个与 int 数组相对应的输出。发布的代码似乎工作得很好。你的问题一定出在其他地方。可能是您实际上并没有发送一个属性List<object>,或者该列表包含不是 int 的东西吗?

只需更改此行:

object valueObj = new List<object> { 1, 2, 3 };

对此:

object valueObj = new List<object> { 1, 2, 3, "Test" };

给了我这个例外。

也许你想要OfType而不是Cast?

如果您在castMethod检索中替换字符串,它会再次“起作用”,但它当然会过滤掉该字符串。

这里需要更多信息。

于 2013-06-14T17:35:00.653 回答
1

嗯,你有来源List<object>,可以得到它的长度。

您可以使用您的类型和长度调用Array.CreateInstance 。然后,您所要做的就是将项目一个接一个地移动。

为什么要为此使用 linq?

于 2013-06-14T17:43:47.667 回答