C# 访问不同类型数组中的项属性
我调用了两个不同的方法,返回两种不同类型的列表。然后,我将这些列表组合成一个数组,从而生成一个包含两种不同类型对象的数组。现在我尝试使用C# 访问不同类型数组中的项属性,c#,arraylist,C#,Arraylist,我调用了两个不同的方法,返回两种不同类型的列表。然后,我将这些列表组合成一个数组,从而生成一个包含两种不同类型对象的数组。现在我尝试使用foreach循环遍历该数组,但是对于数组中的每个项目,我需要访问它的唯一属性。有没有办法做到这一点 List<TypeA> typeAVariable = SomeMethod(); List<TypeB> typeBVariable = AnotherMethod(); var arr = new ArrayList(); arr.
foreach
循环遍历该数组,但是对于数组中的每个项目,我需要访问它的唯一属性。有没有办法做到这一点
List<TypeA> typeAVariable = SomeMethod();
List<TypeB> typeBVariable = AnotherMethod();
var arr = new ArrayList();
arr.AddRange(typeAVariable);
arr.AddRange(typeBVariable);
foreach(var item in arr)
{
if(item.typeOf == typeAVariable)
{
item.typeAVariableProperty;
}
else
{
item.typeBVariableProperty;
}
}
List typeAVariable=SomeMethod();
List typeBVariable=AnotherMethod();
var arr=new ArrayList();
arr.AddRange(类型可变);
arr.AddRange(类型可变);
foreach(arr中的var项目)
{
如果(item.typeOf==类型变量)
{
item.typeAVariableProperty;
}
其他的
{
item.typeBVariableProperty;
}
}
如果您确实需要将不同类型的项目组合到过时的ArrayList
集合中(List
是一个更好的选择),您可以尝试模式匹配以取回itemA
和itemB
:
foreach(var item in arr)
{
if (item is TypeA itemA)
{
itemA.typeAVariableProperty;
}
else if (item is TypeB itemB)
{
itemB.typeBVariableProperty;
}
}
你可以这样写:
switch (item)
{
case TypeA typeA:
typeA.typeAVariableProperty
// do sth
break;
case TypeB typeB:
typeB.typeBVariableProperty
// do sth
break;
}
List<TypeA> typeAVariable = SomeMethod();
List<TypeB> typeBVariable = AnotherMethod();
var combinedList = List<BaseClass>();
combinedList.AddRange(typeAVariable);
combinedList.AddRange(typeBVariable);
foreach (var item in combinedList)
{
if (item is TypeA typeAItem)
{
typeAItem.AnIntroperty;
}
else if (item is TypeB typeBItem)
{
typeBItem.AStringroperty;
}
}
Switch语句可以比if-else结构更快。请参阅:创建一个(抽象)基类,并让两个类都从基类继承。然后创建一个可以同时包含这两种类型的列表
abstract class BaseClass
{
}
class TypeA : BaseClass
{
public int AnIntroperty { get; set; }
}
class TypeB : BaseClass
{
public string AStringroperty { get; set; }
}
然后像这样使用它:
switch (item)
{
case TypeA typeA:
typeA.typeAVariableProperty
// do sth
break;
case TypeB typeB:
typeB.typeBVariableProperty
// do sth
break;
}
List<TypeA> typeAVariable = SomeMethod();
List<TypeB> typeBVariable = AnotherMethod();
var combinedList = List<BaseClass>();
combinedList.AddRange(typeAVariable);
combinedList.AddRange(typeBVariable);
foreach (var item in combinedList)
{
if (item is TypeA typeAItem)
{
typeAItem.AnIntroperty;
}
else if (item is TypeB typeBItem)
{
typeBItem.AStringroperty;
}
}
List typeAVariable=SomeMethod();
List typeBVariable=AnotherMethod();
var combinedList=List();
combinedList.AddRange(typeAVariable);
combinedList.AddRange(类型可变);
foreach(组合列表中的变量项)
{
如果(项目为A型项目)
{
无向性;
}
else if(项目为B型typeBItem)
{
类型bitem.astringproperty;
}
}
我想在检查对象是否为空之前,我会使用as
来强制转换对象。像这样:
foreach(var item in arr)
{
var convertedItem = item as typeAVariable;
if(convertedItem == null)
{
// Do what you need for B type
(typeBVariable)item.typeBVariableProperty;
continue;
}
// Do what you need for A type
convertedItem.typeAVariableProperty;
}为什么需要将它们合并到一个列表中?为什么不使用两个foreach循环,每个列表一个?您是否需要访问每个类型的多个属性,并且这些属性是相同或相似的类型?@ChetanRanpariya我想将它们组合起来,这样我可以使用更少的代码(例如,一个foreach
循环而不是两个)。@Terence我需要访问每个类型的两个属性。谢谢!