I am trying to create an array of the type that is known and currently set to Type
. I have been able to create an IList
of the type but I am still able to convert that to an array of the type, getting object[]
instead.
object propertyValue; //This needs to be an object since it can be set to any object
Type currentType = PropertyInfo.GetType(); //Example: System.String
propertyValue = GetArray(reader, currentType); //What would this look like to make currentType work?
//Reflection occuring later to set propertyValue to attribute of String[]
Here what I got what working with IList
, the issue here is not sure how to cast it to an array of currentType
. I also prefer just getting an array back instead:
private IList GetArray(Reader reader, Type currentType)
{
var returnList = createList(currentType);
//reader loop that appends to list
return returnList;
}
public IList createList(Type currentType)
{
Type genericListType = typeof(List<>).MakeGenericType(currentType);
return (IList)Activator.CreateInstance(genericListType);
}