陣列我有2種類型:轉換動態對象來動態類型的在c#
public class Type1
{
public string Name { get; set; }
}
public class Type2
{
public string Name { get; set; }
}
我有元素的列表(每個元素是一個對象類型)。有些元素可能是數組。 (陣列可以是TYPE1 []或2型[])
我的目標是:
我的元素的列表上1-迭代
-2-確定哪些是TYPE1 []數組PR TYPE2 []數組
3送那些前一陣
的元素名稱值屬性這是我做了什麼:
foreach (var Myobject in MyList)
{
if (myObject.GetType().IsArray)
{
var elementType = myObject.GetType().GetElementType()// should me return the element type, ie Type1 or Type2
//This is where I am stuck, I know that my object is an array but I cannot cast if in type1[] or type2[] array by using elementType
//The following is not working
elementType[] myArrat = (elementType[])myObject;
// And I don't want to hardwrite each case for each possible type like this :
Type1[] myArrat = (Type1[])myObject;
Type2[] myArrat = (Type2[])myObject;
// I want to use the elementType that I got previously
}
}
在此先感謝您的幫助。