I have something like this
enum Animal
{
Cat,
Dog,
Cow,
Pigeon
}
And class
public class Cow
{ }
public class Dog
{ }
public class Cow
{ }
public class Pigeon
{ }
That's what I want to achieve is to dynamically create a class object based on the selected enumu. Enum and class names are always the same. Enum and classes are dozens, so I do not want to add another case for each pair.
e.q.
public object GetAnimal(Animal animal)
used like GetAnimal(Animal.Cow);
should return new Cow class object.
public object GetAnimal(Animal animal)
{
var ns = typeof(Animal).Namespace; //or your classes namespace if different
var typeName = ns + "." + animal.ToString();
return Activator.CreateInstance(Type.GetType(typeName));
}
An alternative is to use custom attributes and extension methods, to add the syntactic equivalent tof a method to the Enum. The accpeted answer is far simpler and therefore probably better, but you may find this of interest.
using System;
using System.Reflection;
namespace CustomAttributes
{
[AttributeUsage(AttributeTargets.Field)]
public class ConstructableEnumAttribute : Attribute
{
public Type Type { get; set; }
public ConstructableEnumAttribute(Type type)
{
this.Type = type;
}
}
public static class AnimalExtension
{
// This is your main mapping method. It doesn't need to be an extension
// method, so you could get the 'Construct(Animal.Cat)' syntax you requested,
// but I like the ability to do:
// Animal myPet = Animal.Cat;
// object animal = myPet.Construct();
public static object Construct(this Animal ofAnimal)
{
object animal = null;
Type typeOfAnimal = GetType(ofAnimal);
if ((null != typeOfAnimal) &&
(!typeOfAnimal.IsAbstract))
{
animal = Activator.CreateInstance(typeOfAnimal);
}
return animal;
}
private static Type GetType(Animal animal)
{
ConstructableEnumAttribute attr = (ConstructableEnumAttribute)
Attribute.GetCustomAttribute
(ForValue(animal), typeof(ConstructableEnumAttribute));
return attr.Type;
}
private static MemberInfo ForValue(Animal animal)
{
return typeof(Animal).GetField(Enum.GetName(typeof(Animal), animal));
}
}
public enum Animal
{
[ConstructableEnum(typeof(Cat))]
Cat,
[ConstructableEnum(typeof(Dog))]
Dog,
[ConstructableEnum(typeof(Cow))]
Cow,
[ConstructableEnum(typeof(Pigeon))]
Pigeon
}
public class Cat
{ }
public class Dog
{ }
public class Cow
{ }
public class Pigeon
{ }
public class Owner
{
Animal pet;
Owner(Animal animal)
{
pet = animal;
}
public void ShowPet()
{
object theCreature = pet.Construct();
Console.WriteLine("My pet is a " + theCreature.GetType().ToString());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With