Type casting inside generic class

  • Thread starter Thread starter Marco Segurini
  • Start date Start date
M

Marco Segurini

Hi,

I am converting a regular class

enum ENUM_TEST
{
test1,
test2,
test3,
}

class enum_no_generic
{
public static bool IsValid(int id)
{
return System.Enum.IsDefined(typeof(ENUM_TEST), (ENUM_TEST)id);
}
}

in a generic class

class enum_t<T>
{
public static bool IsValid(int id)
{
return System.Enum.IsDefined(typeof(T), (T)id); // line 36
}
}

but I get the following build error:

C:\...\Program.cs(36,50): error CS0030: Cannot convert type 'int' to 'T'

that means that the conversion "(T)id" is not permitted.

Is there any way to achieve my goal?

TIA

Marco.
 
Marco,

You don't need that conversion. Enum.IsDefined accepts Object, so you don't
need to cast to anything.

return System.Enum.IsDefined(typeof(T), id);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top