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.List
1[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"> </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.