How to make a type explicitly castable to any enum
up vote
2
down vote
favorite
I have
struct MyType
{
public int value;
}
How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.
The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.
It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)
An example use case :
if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}
Another one,
if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
c#
|
show 5 more comments
up vote
2
down vote
favorite
I have
struct MyType
{
public int value;
}
How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.
The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.
It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)
An example use case :
if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}
Another one,
if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
c#
2
YourMyTypeis no enum, just any value-type. Why should it be castable toenum?
– HimBromBeere
Nov 8 at 10:16
3
But why would you want to do this?intalso is not castable toEnum. As far as I know you can only cast an instance of a specific enum type toEnum. Anintcannot be cast toEnumwithout being cast to a specific enum before.
– René Vogt
Nov 8 at 10:24
1
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
1
Castable to any enum? Including those which arebytebased rather thanint?
– Damien_The_Unbeliever
Nov 8 at 10:42
2
What exactly do you mean by "any enum"? You refer to all enums, e.g.MyEnum, but alsoYourEnumat the same time? That would mean both,(MyEnum) myTypeand(YourEnum)myTypeand even(BindingFlags)myTypewould be valid? Or do you mean a specific enum (AnyEnum)?
– HimBromBeere
Nov 8 at 11:19
|
show 5 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have
struct MyType
{
public int value;
}
How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.
The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.
It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)
An example use case :
if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}
Another one,
if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
c#
I have
struct MyType
{
public int value;
}
How can I define an explicit operator so that it is castable to any enum ? "any" means any enum in existence.
The "convertible to any possible enum" function is already possible if it is an int and not a struct containing int. For example I can always (AnyEnum)myType.value, (SomeOtherEnum)myType.value. But I would like to be able to do (AnyEnum)myType directly without digging an int inside. My idea is naturally, utilizing implicit in some way which will treat the whole thing as int. But unfortunately implicit does not support generic, so I would have to define implicit for every enum I wish to support.
It is fine if this involves some use of C# 7.3's Enum generic constraint. But it seems even with the constraint where T : Enum I cannot cast an int to T by (T)value. ("Cannot convert type int to T)
An example use case :
if(myValue.something == something)
{
switch((MyEnum)myValue)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
else if(myValue.something == something)
{
switch((MyOtherEnum)myValue)
{
case MyOtherEnum.A : ...
case MyOtherEnum.B : ...
}
}
Another one,
if(myValue.ValidInEnum<MyEnum>(out MyEnum validEnum)) //converted from inside with some additional logic
{
switch(validEnum)
{
case MyEnum.A : ...
case MyEnum.B : ...
}
}
c#
c#
edited Nov 9 at 9:40
asked Nov 8 at 10:13
5argon
1,23511430
1,23511430
2
YourMyTypeis no enum, just any value-type. Why should it be castable toenum?
– HimBromBeere
Nov 8 at 10:16
3
But why would you want to do this?intalso is not castable toEnum. As far as I know you can only cast an instance of a specific enum type toEnum. Anintcannot be cast toEnumwithout being cast to a specific enum before.
– René Vogt
Nov 8 at 10:24
1
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
1
Castable to any enum? Including those which arebytebased rather thanint?
– Damien_The_Unbeliever
Nov 8 at 10:42
2
What exactly do you mean by "any enum"? You refer to all enums, e.g.MyEnum, but alsoYourEnumat the same time? That would mean both,(MyEnum) myTypeand(YourEnum)myTypeand even(BindingFlags)myTypewould be valid? Or do you mean a specific enum (AnyEnum)?
– HimBromBeere
Nov 8 at 11:19
|
show 5 more comments
2
YourMyTypeis no enum, just any value-type. Why should it be castable toenum?
– HimBromBeere
Nov 8 at 10:16
3
But why would you want to do this?intalso is not castable toEnum. As far as I know you can only cast an instance of a specific enum type toEnum. Anintcannot be cast toEnumwithout being cast to a specific enum before.
– René Vogt
Nov 8 at 10:24
1
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
1
Castable to any enum? Including those which arebytebased rather thanint?
– Damien_The_Unbeliever
Nov 8 at 10:42
2
What exactly do you mean by "any enum"? You refer to all enums, e.g.MyEnum, but alsoYourEnumat the same time? That would mean both,(MyEnum) myTypeand(YourEnum)myTypeand even(BindingFlags)myTypewould be valid? Or do you mean a specific enum (AnyEnum)?
– HimBromBeere
Nov 8 at 11:19
2
2
Your
MyType is no enum, just any value-type. Why should it be castable to enum?– HimBromBeere
Nov 8 at 10:16
Your
MyType is no enum, just any value-type. Why should it be castable to enum?– HimBromBeere
Nov 8 at 10:16
3
3
But why would you want to do this?
int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.– René Vogt
Nov 8 at 10:24
But why would you want to do this?
int also is not castable to Enum. As far as I know you can only cast an instance of a specific enum type to Enum. An int cannot be cast to Enum without being cast to a specific enum before.– René Vogt
Nov 8 at 10:24
1
1
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
1
1
Castable to any enum? Including those which are
byte based rather than int?– Damien_The_Unbeliever
Nov 8 at 10:42
Castable to any enum? Including those which are
byte based rather than int?– Damien_The_Unbeliever
Nov 8 at 10:42
2
2
What exactly do you mean by "any enum"? You refer to all enums, e.g.
MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?– HimBromBeere
Nov 8 at 11:19
What exactly do you mean by "any enum"? You refer to all enums, e.g.
MyEnum, but also YourEnum at the same time? That would mean both, (MyEnum) myType and (YourEnum)myType and even (BindingFlags)myType would be valid? Or do you mean a specific enum (AnyEnum)?– HimBromBeere
Nov 8 at 11:19
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
4
down vote
If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:
static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}
enum MyEnum
{
a = 0,
b = 1
}
struct MyType
{
public int value;
public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}
The output is:
b
If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:
public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}
Then you can write:
MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
This converts toMyEnum, but not anyenum.
– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
|
show 5 more comments
up vote
1
down vote
It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:
var anyEnum = (AnyEnum)myType.value;
Another way would be using something like a ToEnum method maybe:
struct MyType
{
int value;
public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}
Usage:
var anyEnum = myType.ToEnum<AnyEnum>();
You could also create an explicit cast to your value type:
public static explicit operator int(MyType myType)
{
return myType.value;
}
Then you can do:
var anyEnum = (AnyEnum)(int)myType;
I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.
Personal Note
I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:
static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}
enum MyEnum
{
a = 0,
b = 1
}
struct MyType
{
public int value;
public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}
The output is:
b
If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:
public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}
Then you can write:
MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
This converts toMyEnum, but not anyenum.
– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
|
show 5 more comments
up vote
4
down vote
If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:
static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}
enum MyEnum
{
a = 0,
b = 1
}
struct MyType
{
public int value;
public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}
The output is:
b
If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:
public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}
Then you can write:
MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
This converts toMyEnum, but not anyenum.
– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
|
show 5 more comments
up vote
4
down vote
up vote
4
down vote
If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:
static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}
enum MyEnum
{
a = 0,
b = 1
}
struct MyType
{
public int value;
public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}
The output is:
b
If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:
public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}
Then you can write:
MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();
If you want to convert from an enum directly instead of having to access the "value" member of your struct, yes, it is possible, here is an example:
static void Main(string args)
{
MyType myType;
myType.value = 1;
MyEnum myEnum = (MyEnum) myType;
Console.WriteLine(myEnum);
Console.ReadLine();
}
enum MyEnum
{
a = 0,
b = 1
}
struct MyType
{
public int value;
public static explicit operator MyEnum(MyType myType)
{
return (MyEnum)myType.value;
}
}
The output is:
b
If you want to cast from any type of enum directly to your struct, I don't think that would be possible, you can look into generics as suggested in the other answer, or maybe write a little extension helper for your type:
public static class MyTypeExtensions
{
public static T ToAnyEnum<T>(this MyType myType)
{
return (T)Enum.ToObject(typeof(T), myType.value);
}
}
Then you can write:
MyType myType;
myType.value = 1;
MyEnum myEnum = myType.ToAnyEnum<MyEnum>();
MyOtherEnum myOtherEnum = myType.ToAnyEnum<MyOtherEnum>();
edited Nov 8 at 11:40
answered Nov 8 at 10:19
Isma
6,60541733
6,60541733
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
This converts toMyEnum, but not anyenum.
– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
|
show 5 more comments
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
This converts toMyEnum, but not anyenum.
– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– SazooCat
Nov 8 at 10:21
1
1
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
MyEnum doesn't need to inherit from int
– Antoine V
Nov 8 at 10:23
You are right, I changed it...
– Isma
Nov 8 at 10:28
You are right, I changed it...
– Isma
Nov 8 at 10:28
1
1
This converts to
MyEnum, but not any enum.– marsze
Nov 8 at 10:59
This converts to
MyEnum, but not any enum.– marsze
Nov 8 at 10:59
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
Well, he called it "AnyEnum" but in his quesiton he is doing an actual cast from (AnyEnum) using the value of the struct so I answered accordingly.
– Isma
Nov 8 at 11:14
|
show 5 more comments
up vote
1
down vote
It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:
var anyEnum = (AnyEnum)myType.value;
Another way would be using something like a ToEnum method maybe:
struct MyType
{
int value;
public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}
Usage:
var anyEnum = myType.ToEnum<AnyEnum>();
You could also create an explicit cast to your value type:
public static explicit operator int(MyType myType)
{
return myType.value;
}
Then you can do:
var anyEnum = (AnyEnum)(int)myType;
I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.
Personal Note
I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.
add a comment |
up vote
1
down vote
It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:
var anyEnum = (AnyEnum)myType.value;
Another way would be using something like a ToEnum method maybe:
struct MyType
{
int value;
public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}
Usage:
var anyEnum = myType.ToEnum<AnyEnum>();
You could also create an explicit cast to your value type:
public static explicit operator int(MyType myType)
{
return myType.value;
}
Then you can do:
var anyEnum = (AnyEnum)(int)myType;
I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.
Personal Note
I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.
add a comment |
up vote
1
down vote
up vote
1
down vote
It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:
var anyEnum = (AnyEnum)myType.value;
Another way would be using something like a ToEnum method maybe:
struct MyType
{
int value;
public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}
Usage:
var anyEnum = myType.ToEnum<AnyEnum>();
You could also create an explicit cast to your value type:
public static explicit operator int(MyType myType)
{
return myType.value;
}
Then you can do:
var anyEnum = (AnyEnum)(int)myType;
I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.
Personal Note
I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.
It's not possible. You cannot create a cast operator for any enum type, only for concrete enum types I'm afraid you would have go through the "big hassle" of doing:
var anyEnum = (AnyEnum)myType.value;
Another way would be using something like a ToEnum method maybe:
struct MyType
{
int value;
public T ToEnum<T>() where T : struct
{
return (T)Enum.ToObject(typeof(T), this.value);
}
}
Usage:
var anyEnum = myType.ToEnum<AnyEnum>();
You could also create an explicit cast to your value type:
public static explicit operator int(MyType myType)
{
return myType.value;
}
Then you can do:
var anyEnum = (AnyEnum)(int)myType;
I admit it's not really much shorter, but this would allow to make value private for example or do any other custom logic in the conversion.
Personal Note
I don't believe this kind of design makes much sense. It just tries to answer the original question. It was interesting to think about it.
edited Nov 8 at 11:58
answered Nov 8 at 11:04
marsze
3,95631640
3,95631640
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205568%2fhow-to-make-a-type-explicitly-castable-to-any-enum%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Your
MyTypeis no enum, just any value-type. Why should it be castable toenum?– HimBromBeere
Nov 8 at 10:16
3
But why would you want to do this?
intalso is not castable toEnum. As far as I know you can only cast an instance of a specific enum type toEnum. Anintcannot be cast toEnumwithout being cast to a specific enum before.– René Vogt
Nov 8 at 10:24
1
It sounds like you'd like to implement a generic operator. But that's not supported in c#.
– René Vogt
Nov 8 at 10:26
1
Castable to any enum? Including those which are
bytebased rather thanint?– Damien_The_Unbeliever
Nov 8 at 10:42
2
What exactly do you mean by "any enum"? You refer to all enums, e.g.
MyEnum, but alsoYourEnumat the same time? That would mean both,(MyEnum) myTypeand(YourEnum)myTypeand even(BindingFlags)myTypewould be valid? Or do you mean a specific enum (AnyEnum)?– HimBromBeere
Nov 8 at 11:19