mardi 12 mai 2015

How to pass enum value to method through reflection

class Program
{
    static void Main(string[] args)
    {
        Type[] types = Assembly.GetExecutingAssembly().GetTypes();

        Type TEnum = types.Where(d => d.Name == "TEnum").FirstOrDefault();
        var values = TEnum.GetEnumValues();
        var error = new object();
        foreach (var value in values)
        {
            if (value.ToString() == "Test2")
            {
                error = value;

            }
        }

        TestMethod("A",ref error);
    }

    public static void TestMethod(string a, ref TEnum b)
    {

    }

    public enum TEnum
    {
        Test, 
        Test2
    }

}

In the above code I am trying to pass enum which I got from refection. This is just a sample code actually the TestMethod(string a, ref TEnum b) and enum TEnum are in different assembly which I am loading through Reflection. In this sample how I can pass enum as parameter to method. Currently I am getting compilation error for this.

Thanks in advance





Aucun commentaire:

Enregistrer un commentaire