Use a variable as a type and create it

First of all, I want to say that I am new to C #, so this question may seem completely inoperative.

I have a set of enumerations called ShapeType:

Cube, Sphere, Rectangle, Ellipse

And a method to return a random value from enumerations:

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

Each enumerated has a corresponding concrete class. And the question I'm interested in is creating an instance of the class using the random enumerated value randomShape, sort of like:

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

Is this possible or is it just wishful thinking?

+6
source share
4 answers

You can use a dictionary to retrieve the factory function for each enumeration value:

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;
}
+3
source

factory :

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

Create a dictionary with the enumvalue key and enter it as a 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;
}
+1
source

Instead

enum ShapeType { Cube, Sphere, Rectangle, Ellipse }

You can use something like

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) { }
}

this will allow you to do something like:

Console.WriteLine((object)(Figure)1); // Cube
Console.WriteLine((object)(Figure)2); // Sphere

I use this approach in many places instead enum, which is often insufficient (because it should contain more information than just the name and int).

0
source

Source: https://habr.com/ru/post/1016990/


All Articles