Hi
Suppose enum:
public enum SysLogsAppTypes { None, MonitorService, MonitorTool };
and here is a function to convert from the ToString()
representation back to enum
:
private SysLogsAppTypes Str2SysLogsAppTypes(string str)
{
try
{
SysLogsAppTypes res = (SysLogsAppTypes)Enum
.Parse(typeof(SysLogsAppTypes), str);
if (!Enum.IsDefined(typeof(SysLogsAppTypes), res))
return SysLogsAppTypes.None;
return res;
}
catch
{
return SysLogsAppTypes.None;
}
}
Is there a way to make this Generic ??
I tried:
private T Str2enum<T>(string str)
{
try
{
T res = (T)Enum.Parse(typeof(T), str);
if (!Enum.IsDefined(typeof(T), res)) return T.None;
return res;
}
catch
{
return T.None;
}
}
but I get:
'T' is a 'type parameter', which is not valid in the given context
where there is T.None
Any help ?
Thanks
Micha