I searched but didn't find correct answer.
Assume that we have a class like:
public sealed Class Foo{
public static readonly Foo x;
public static readonly Foo y;
public static readonly Foo z;
public static readonly Foo t;
public string Name{get;}
public int Value{get;}
public Foo(string name,int val){//properties sets here}
}
And I have an enum
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?
I want to `Foo.x` as return object not class of `Foo`.
Consider that `Foo` is using for designating a level. For example `log4net.Core.Level` has 17 field like `Level.All`, `Level.Info`,`Level.Off` etc. to determine the level of logging. I want to return `Level` class' chosen field by passing `enum` parameter. For example I call `GetFoo` method with `FooFinder.x` parameter. And I want `GetFoo` metod to return `Foo.x` object. Is it possible? – CagdasA Mar 22 '16 at 13:58