mardi 22 mars 2016

C# Reflection of a class field

I searched but didn't find correct answer..

Assume that we have a class like:

public sealed Class Foo{
     public static readonly x;
     public static readonly y;
     public static readonly z;
     public static readonly t;

     public string Name{get;}
     public int Value{get;}
     public Foo(string name,int val){//properties sets here}
} 

And I have a Enum class like:

Public Enum FooFinder{
         x,
         y,
         z,
         t

}

this Foo class in a different library called FooLib.dll and FooFinder created by me..

I want to return Foo.x, Foo.y types using my enum class.

 private static Foo GetFoo (FooFinder Level)
    {
        FieldInfo[] props = typeof(Foo).GetFields();

        foreach (FieldInfo item in props)
        {

            if (item.Name == Enum.GetName(typeof(FooFinder), Level))
            {
                 //I could create an instance of Foo with Activator.CreateInstance() but I want to return Foo class' chosen field by FooFinder enum
            }
        }

    }

I hope I could explain what I want to do.. So how can I get Foo class' chosen field by reflection? or Can I get ?

Best Regards, Cagdas





Aucun commentaire:

Enregistrer un commentaire