C# Create method with generic enum as parameter

How can I pass a generic Enum to a method?  I want to pass any enum object to the below method.  Receiving error on "enum" parameter.  Error is "type or namespace enumName not found".
        public static bool FindExcludedFields(string value, Enum enumName)
        {
            try
            {
                enumName column =
                    (enumName)Enum.Parse(typeof(enumName), value);

                //Enumerators.ExcludedItemsFields column =
                //    (Enumerators.ExcludedItemsFields)Enum.Parse(typeof(Enumerators.ExcludedItemsFields), value);

                //if (Enum.IsDefined(typeof(Enumerators.ExcludedItemsFields), column))
                if (Enum.IsDefined(typeof(enumName), column))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
You would need to pass the type of the enum rather than the enum itself. Change your function declaration as follows then alter your code body accordingly.
public static bool FindExcludedFields(string value, Type enumType)
{
    ...
}

Open in new window

0
CipherISAuthor Commented:
Made the below changes but "enumType" in the try is giving an error -> "Type or namespace enumType could not be found are you missing a directive or namespace?
        public static bool FindExcludedFields(string value, Type enumType)
        {
            try
            {
                enumType column = (enumType)Enum.Parse(typeof(enumType), value);
                if (Enum.IsDefined(typeof(enumType), column))
                    return true;
                else
                    return false;

            }
            catch (Exception ex)
            {
                return false;
            }
        }

Open in new window

0
CipherISAuthor Commented:
This works.
    public static class EnumValidation
    {
        public static bool FindExcludedFields<T>(string value)
        {
            try
            {
                if (typeof(T).IsEnum)
                {
                    T column = (T)Enum.Parse(typeof(T), value);
                    if (Enum.IsDefined(typeof(T), column))
                        return true;
                    else
                        return false;
                }
                else
                {
                    throw new ApplicationException("Enum is not valid");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

Open in new window

0
CipherISAuthor Commented:
@Russ - any thoughts?
0
it_saigeDeveloperCommented:
The simplest form is this:
static class Extensions
{
    public static bool FindExcludedFields(this Enum source, string value)
    {
        try
        {
            return Enum.IsDefined(source.GetType(), Enum.Parse(source.GetType(), value));
        }
        catch (Exception)
        {
            return false;
        }
    }
}

Open in new window

Proof of concept:
using System;

namespace EE_Q29091702
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"October is a DayOfWeek {DayOfWeek.Saturday.FindExcludedFields("October")}");
            Console.WriteLine($"Sunday is a DayOfWeek {DayOfWeek.Saturday.FindExcludedFields("Sunday")}");
            Console.ReadLine();
        }
    }

    static class Extensions
    {
        public static bool FindExcludedFields(this Enum source, string value)
        {
            try
            {
                return Enum.IsDefined(source.GetType(), Enum.Parse(source.GetType(), value));
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.