C# – Cannot access Object fields using fieldinfo

cobservablecollectionreflection

I have an method that takes in an observable collection (returned from a webservice) of objects and analyzes them according to their attributes.

Here is the code snippet from the method

private double analyze(ObservableCollection mobjColl)
{

        FieldInfo fi = null;

        foreach (MyApp.MyObj oi in mobjColl)
        {

        if(oi.pressure.Equals("high"){

            fi = oi.GetType().GetField("air");

            .....
        }
        }
        return someval;
    }

My problem is that the fieldinfo fi is always null. I can access every field of the object (within foreach) using the objects name however the fieldinfo object is never populated. I even tried using GetFields method but it does not return an array…

P.S : the object fields are public. Using bindingflags in getfield didnt help either.

Best Answer

I don't believe objects returned from web services expose public fields. You might be thinking of properties instead. If you try GetProperty("air") you'll probably get something back.