使用变量作为类型并实例化它

时间:2017-05-04 12:29:49

标签: c#

首先,我想说我是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;
}

这可能还是只是一厢情愿的想法?

4 个答案:

答案 0 :(得分:3)

您可以使用字典为枚举的每个值检索 factory 函数:

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;
}

答案 1 :(得分:2)

您需要使用factory method pattern

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();
         ...
    }
}

答案 2 :(得分:1)

创建一个以enumvalue为键的词典,并输入值。

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;
}

答案 3 :(得分:0)

而不是

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)。