This question relates to casting of enums within generic methods
Given an enum
public enum Crustaceans
{
Frog = 1,
Toad = 4
}
I can create an instance of my enum simply enough
short val = 4;
Crustaceans crusty = (Crustaceans) val;
However, if
short val = 4;
object obj = (object) val;
Crustaceans crusty = (Crustaceans)obj;
a runtime exception is thrown attempting to perform the initialisation of crusty.
Can anyone explain why this is happening, and why it is not legal to do such a thing.
Not that I really wanted to do this, but I cam across an issue when trying to get something similar happening with generics and effectively that is what is happening under the covers. i.e.
public T dosomething<T>(short val) where T : new()
{
T result = (T)(object) val;
return result;
}
So what I am attempting to do is have a generic function that works with enums and non-enums (not so critical-but would be nice) that can be set to a short value without throwing an exception and actually initialising the correct enum value.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Something like this probably will help you:
public T dosomething<T>(object o) { T enumVal= (T)Enum.Parse(typeof(T), o.ToString()); return enumVal; }
But this will work only with enums, for clear reason of using Enum.Parse(..)
And use this like, for example:
object o = 4; dosomething<Crustaceans>(o);
That will return Toad
in your case.
In case of integral types boxed as objects the correct way to do the conversion is using Enum.ToObject method:
public T Convert<T>(object o) { T enumVal= (T)Enum.ToObject(typeof(T), o); return enumVal; }
There are cases when you can not use Generics (like in a WPF Converter when you get the value as an object
).
In this case you can not cast to int
because the enum type may not be an int
.
This is a general way to do it without Generics.
The Example is given inside a WPF Converter, but the code inside is general:
using System;
using System.Windows;
using System.Windows.Data;
.
.
.
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var enumType = value.GetType();
var underlyingType = Enum.GetUnderlyingType(enumType);
var numericValue = System.Convert.ChangeType(value, underlyingType);
return numericValue;
}
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