如何在 C# 中修改具有公共超类的对象列表的成员?
How can I modify a member of an object list with a common superclass in C#?
我在 Visual Studio 中实现了一个 WindowsForms 应用程序。现在我有一份玩具清单:
List <Toy> toys;
Toy
是抽象的class,class像Car
、Submarine
等都是从它派生出来的。该列表当然可以包含 Toy
类型的任何对象。由于我缺乏 C# 经验,我不确定如何修改此列表中的对象,即。 e.更改特定于类型的 属性。编译器只知道列表包含 Toy
个对象,无法访问 Submarine
个对象的字段。所以我不能简单地从列表中取出一些元素,调用 setter 并完成它。转换只会让我获得转换为某种类型的列表对象的副本。我怎样才能做到这一点?
你必须施放物品:
Engine door = toys.OfType<Car>().Select(c => c.Engine).FirstOrDefault();
如果您不熟悉 Lync 或者想对项目做更多的事情,您也可以按 'old school' 方式循环执行。
给定样本 类:
public abstract class Toy
{
public string Manufacturer { get; set; }
}
public class Elephant : Toy
{
public int TrunkLength { get; set; }
}
public class Car : Toy
{
public Color Color { get; set; }
}
您可以像这样添加项目,然后根据它们的类型修改它们:
var toys = new List<Toy>
{
new Car {Color = Color.Red, Manufacturer = "HotWheels"},
new Elephant {TrunkLength = 36, Manufacturer = "Steiff Harlequin"}
};
foreach (var toy in toys)
{
var car = toy as Car;
if (car != null)
{
// Set properties or call methods on the car here
car.Color = Color.Blue;
// Continue the loop
continue;
}
var elephant = toy as Elephant;
if (elephant != null)
{
// Do something with the elephant object
elephant.TrunkLength = 48;
// Continue the loop
continue;
}
}
或者您可以使用 'switch' 语句和一些转换,这可能更具可读性:
foreach (var toy in toys)
{
switch (toy.GetType().Name)
{
case "Car":
((Car) toy).Color = Color.Blue;
break;
case "Elephant":
var elephant = toy as Elephant;
elephant.TrunkLength = 48;
elephant.Manufacturer = "Made in China";
break;
}
}
所以...你基本上有这样的情况?
public abstract class Toy
{
}
public class Submarine : Toy
{
public bool IsOccupiedByPretendPeople { get; set; }
}
你还有
List<Toy> toys;
而您想获取 Submarine
class 的所有实例?
这应该可以解决问题:
IEnumerable<Submarine> submarines = toys.OfType<Submarine>();
如果您发布了 Toy
class 的示例(如您问题的评论中所述),我可以进一步帮助您完成您想要完成的任务。
当我们有一个继承家族并且我们需要使用来自其中一个子类型的特定特征时,我们也可以使用 is 运算符:这是一个例子
public抽象class玩具
{
}
public class Submarine : Toy
{
}
public class Airwolf : Toy
{
public Boolean ActivateMissiles()
{
return true;
}
}
class Program
{
static void Main(string[] args)
{
List<Toy> myToys = new List<Toy>();
myToys.Add(new Submarine());
myToys.Add(new Airwolf());
// Looking for airwolves
foreach (Toy t in myToys)
{
if (t is Airwolf)
{
Airwolf myWolf = (Airwolf)t;
myWolf.ActivateMissiles();
}
}
}
}
我在 Visual Studio 中实现了一个 WindowsForms 应用程序。现在我有一份玩具清单:
List <Toy> toys;
Toy
是抽象的class,class像Car
、Submarine
等都是从它派生出来的。该列表当然可以包含 Toy
类型的任何对象。由于我缺乏 C# 经验,我不确定如何修改此列表中的对象,即。 e.更改特定于类型的 属性。编译器只知道列表包含 Toy
个对象,无法访问 Submarine
个对象的字段。所以我不能简单地从列表中取出一些元素,调用 setter 并完成它。转换只会让我获得转换为某种类型的列表对象的副本。我怎样才能做到这一点?
你必须施放物品:
Engine door = toys.OfType<Car>().Select(c => c.Engine).FirstOrDefault();
如果您不熟悉 Lync 或者想对项目做更多的事情,您也可以按 'old school' 方式循环执行。
给定样本 类:
public abstract class Toy
{
public string Manufacturer { get; set; }
}
public class Elephant : Toy
{
public int TrunkLength { get; set; }
}
public class Car : Toy
{
public Color Color { get; set; }
}
您可以像这样添加项目,然后根据它们的类型修改它们:
var toys = new List<Toy>
{
new Car {Color = Color.Red, Manufacturer = "HotWheels"},
new Elephant {TrunkLength = 36, Manufacturer = "Steiff Harlequin"}
};
foreach (var toy in toys)
{
var car = toy as Car;
if (car != null)
{
// Set properties or call methods on the car here
car.Color = Color.Blue;
// Continue the loop
continue;
}
var elephant = toy as Elephant;
if (elephant != null)
{
// Do something with the elephant object
elephant.TrunkLength = 48;
// Continue the loop
continue;
}
}
或者您可以使用 'switch' 语句和一些转换,这可能更具可读性:
foreach (var toy in toys)
{
switch (toy.GetType().Name)
{
case "Car":
((Car) toy).Color = Color.Blue;
break;
case "Elephant":
var elephant = toy as Elephant;
elephant.TrunkLength = 48;
elephant.Manufacturer = "Made in China";
break;
}
}
所以...你基本上有这样的情况?
public abstract class Toy
{
}
public class Submarine : Toy
{
public bool IsOccupiedByPretendPeople { get; set; }
}
你还有
List<Toy> toys;
而您想获取 Submarine
class 的所有实例?
这应该可以解决问题:
IEnumerable<Submarine> submarines = toys.OfType<Submarine>();
如果您发布了 Toy
class 的示例(如您问题的评论中所述),我可以进一步帮助您完成您想要完成的任务。
当我们有一个继承家族并且我们需要使用来自其中一个子类型的特定特征时,我们也可以使用 is 运算符:这是一个例子
public抽象class玩具 { }
public class Submarine : Toy
{
}
public class Airwolf : Toy
{
public Boolean ActivateMissiles()
{
return true;
}
}
class Program
{
static void Main(string[] args)
{
List<Toy> myToys = new List<Toy>();
myToys.Add(new Submarine());
myToys.Add(new Airwolf());
// Looking for airwolves
foreach (Toy t in myToys)
{
if (t is Airwolf)
{
Airwolf myWolf = (Airwolf)t;
myWolf.ActivateMissiles();
}
}
}
}