[英]Use variable as type and instantiate it
首先,我想說我是C#的新手,所以這個問題似乎完全偏離了軌道。
我有一組名為ShapeType的枚舉:
Cube, Sphere, Rectangle, Ellipse
以及從枚舉中返回隨機值的方法:
private static ShapeType GetRandomShape()
{
Array values = Enum.GetValues(typeof(ShapeType));
Random random = new Random();
ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
return randomShape;
}
每個可枚舉的都有相應的具體類。 我想知道的問題是,如果你可以使用隨機可枚舉值randomShape來實例化一個類,有點像這樣:
private static Shape GetRandomShape()
{
Array values = Enum.GetValues(typeof(ShapeType));
Random random = new Random();
ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
Shape shape = new randomShape(); // *Here use the randomShape-variable as type*
return shape;
}
這可能還是只是一廂情願的想法?
您可以使用字典為枚舉的每個值檢索工廠函數:
static readonly Dictionary<ShapeType, Func<Shape>> _factoryLookup = new Dictionary<ShapeType, Func<Shape>>
{
[ShapeType.Cube] = () => new Cube(),
[ShapeType.Ellipse] = () => new Ellipse(),
[ShapeType.Rectangle] = () => new Rectangle(),
[ShapeType.Sphere] = () => new Sphere(),
};
static readonly Random random = new Random();
private static Shape GetRandomShape()
{
Array values = Enum.GetValues(typeof(ShapeType));
ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
Func<Shape> factory = _factoryLookup[randomShape];
Shape shape = factory();
return shape;
}
您需要使用工廠方法模式 :
public class Shape {}
public class Cube : Shape {}
public class Sphere : Shape {}
public class Rectangle : Shape {}
public class Ellipse : Shape {}
public Shape randomShape(ShapeType shapeType)
{
switch(shapeType)
{
case ShapeType.Cube:
return new Cube();
...
}
}
使用enumvalue作為鍵創建一個Dictionary,並鍵入value。
Dictionary<ShapeType, Type> dic = new Dictionary<ShapeType, Type>();
dic.Add(ShapeType.Cube, typeof(Cube));
// ...
private static Shape GetRandomShape()
{
Array values = Enum.GetValues(typeof(ShapeType));
Random random = new Random();
ShapeType randomShape = (ShapeType)values.GetValue(random.Next(values.Length));
Shape shape = Activator.CreateInstance(dic[randomShape]); // *Here use the randomShape-variable as type*
return shape;
}
代替
enum ShapeType { Cube, Sphere, Rectangle, Ellipse }
你可以使用類似的東西
abstract class Figure
{
protected int id;
public static implicit operator int(Figure figure) => figure.id;
public static implicit operator Figure(int value) => Figures().First(figure => figure.id == value);
protected Figure(int id)
{
this.id = id;
}
public static Cube Cube { get; } = new Cube(1);
public static Sphere Sphere { get; } = new Sphere(2);
public static IEnumerable<Figure> Figures()
{
yield return Cube;
yield return Sphere;
}
public override string ToString() => this.GetType().ToString();
}
class Cube : Figure
{
public Cube(int id) : base(id) { }
}
class Sphere : Figure
{
public Sphere(int id) : base(id) { }
}
這將讓你做類似的事情:
Console.WriteLine((object)(Figure)1); // Cube
Console.WriteLine((object)(Figure)2); // Sphere
我在許多地方使用這種方法而不是enum
,這通常是不夠的(因為它需要保存額外的信息,而不僅僅是名稱和int
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.