0

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?

CagdasA
  • 3
  • 2
  • Could be [useful to know](https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx). As for question, you don't pass instance to the method, so it must take instance from somewhere. Only you know from where. – Sinatr Mar 22 '16 at 12:22
  • 1
    What is the type of all these static fields (x, y, z, t)? – Sebastian Schumann Mar 22 '16 at 12:47
  • You meant you want to get the _static value_ of that field or are you having trouble finding the right field? If so then it's a duplicate of [this question](http://stackoverflow.com/questions/5898385) – D Stanley Mar 22 '16 at 12:53
  • "I searched but didn't find correct answer." What did you search for? The top 4 search results for search results [c# reflection get static field value](https://www.google.com/search?q=c%23%20reflection%20get%20static%20field%20value) all seem to have valid answers. – D Stanley Mar 22 '16 at 12:57
  • @Verarind I added type of field. (Thank you) – CagdasA Mar 22 '16 at 13:08
  • @DStanley I want to return "Foo.x" or "Foo.y" itself as object. Not value of Foo.x. I could create an instance of Foo class but I want instance of one of Foo class' field. – CagdasA Mar 22 '16 at 13:20
  • @CagdasA I'm still confused. You mean the _string_ "Foo.x"? The type of `x` _is_ a `Foo` so returning it's _value_ will give you a `Foo` instance (assuming it's been set to a valid value somewhere). – D Stanley Mar 22 '16 at 13:36
  • @DStanley I didn't mean any string things. I'm new at here and I'm just learning how to use this site. Sorry about that.
    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
  • @CagdasA I'm sorry, but I still don't understand the distinction between "Foo.x" and the _value_ of the static field `x` that you'd get from reflection. Either I'm missing something or you expect them to be different. – D Stanley Mar 22 '16 at 14:11

1 Answers1

0

Maybe you can use:

private static Foo GetFoo (FooFinder Level)
{
    var fieldInfo = typeof(Foo).GetField(Enum.GetName(typeof(FooFinder), level), BindingFlags.Static | BindingFlags.Public);
    return fieldInfo?.GetValue(null) as Foo;
}
Sebastian Schumann
  • 3,204
  • 19
  • 37