Given a constructor
public MyObject(int id){
ID = id;
}
And two enums:
public enum MyEnum1{
Something = 1,
Anotherthing = 2
}
public enum MyEnum2{
Dodo = 1,
Moustache= 2
}
Is it possible to pass in a generic enum as a parameter of the constructor? I'm looking for a solution along the lines of:
public MyObject(enum someEnum){
ID = (int)someEnum;
}
So you can do:
var newObject = new MyObject(MyEnum1.Something);
var anotherObject = new MyObject(MyEnum2.Dodo);
6 Answers 6
Another option would be:
public MyObject(Enum someEnum){
ID = Convert.ToInt32(someEnum);
}
This way you can use it like you requested without having to cast to int each time you call your contstructors:
var newObject = new MyObject(MyEnum1.Something);
var anotherObject = new MyObject(MyEnum2.Dodo);
1 Comment
Why do you want to pass the enums, while you could pass integers ?
var newObject = new MyObject((int)MyEnum1.Something);
var anotherObject = new MyObject((int)MyEnum2.Dodo);
and use your first constructor :
public MyObject(int id){
ID = id;
}
3 Comments
FlagsJust use a generic constructor:
class MyObject<T> {
public MyObject(T someEnum) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("Not an enum");
ID = Convert.ToInt32(someEnum);
}
}
Now you can easily call it like this:
var m = new MyObject<MyEnum>(MyEnum1.Something);
But easier would be to pass the enum as integer to the constructor as mentioned in other answers.
EDIT: As of C# 7.3 you can use an enum-constraint right away:
class MyObject<T> where T: Enum { ... }
3 Comments
Enum with this approach making it possible to pass instances of int to the method as well.I have tried to do this with Attributes and it seems Enum as parameter in Attributes are not allowed. Therefore I had to do this workaround:
public MyCustomAttribute(object myEnumParameter)
{
if (myEnumParameteris not Enum)
{
throw new ArgumentException("myEnumParameter is not an enum",
nameof(myEnumParameter));
}
MyEnumProperty = (Enum)myEnumParameter;
}
Comments
Well, if you really need to make this call generic for a wide variety of types, then IMHO you should use:
- Type.IsEnum to check if your argument is really an
Enum; - Enum.GetUnderlyingType to know what type is your argument is based on (it's not necessarily an
Int32); Now cast your object.
public static Int32 GetAnInt<T>(T arg) where T : struct { if ((typeof(T).IsEnum)) { var underlyingType = typeof(T).GetEnumUnderlyingType(); if (underlyingType == typeof(Int32) || underlyingType == typeof(Int16)) //etc. { try { dynamic value = arg; var result = (Int32)value; // can throw InvalidCast! return result; } catch { throw; } } else { throw new InvalidCastException("Underlying type is certainly not castable to Int32!"); } } else { throw new InvalidCastException("Not an Enum!"); } }That way you achieve the beautiful syntax of:
var j = GetAnInt(MyEnum.FirstValue);
2 Comments
Are you using properties enum or parameters.
public enum Enum1{}
public Enum1 enum1 { get;set; }
public MyObject()
{
ID = (int)enum1;
}
Just try it. I hope it is useful.
MyBase b = new MySub<EnumA>(EnumA.SomeValue); int n = b.ID;new MyObject((int)MyEnum1.Something);?enumand pass that to a factory that in turn creates the right thing with the rightenumparameter.var newObject = myFactory.Create(ObjectType.One).