0

I have been looking for a while - and I cant find an answer that works....

I am just trying to to find out the type of a variable or property in a class using reflection...

   foreach (XElement items in nodes)
                {
                    Game newGame = new Game();
                    FieldInfo[] fields = newGame.GetType().GetFields(BindingFlags.Instance |
                       BindingFlags.Static |
                       BindingFlags.NonPublic |
                       BindingFlags.Public);

                    foreach(XAttribute item in items.Attributes())
                    {
                        foreach (FieldInfo f in fields)
                        {

                            if (f.Name.Remove(0,1) == item.Name.LocalName)
                            {

                                if (GetTypeOrUnderlyingType(f) == typeof(Int32))
                                {
                                    Type type = typeof(Int32).DeclaringType;
                                    f.SetValue(newGame, Convert.ChangeType(item.Value, type));
                                }
                            }
                        }
                    }
                }

 public Type GetTypeOrUnderlyingType(object o)
        {
            Type type = o.GetType();
            if (!type.IsGenericType) { return type; }
            return type.GetGenericArguments()[0];
        }

And Game is a generated class via linq... I just want to get the type of the field so I know whether i need to cast my xml item.value....

4
  • 1
    Any reason you're using reflection for this instead of typeof or a.var1.GetType() Commented Jun 27, 2013 at 4:41
  • well im using reflection to get and set the fields...as I am reading in an xml file and then trying to set fields based on xml using reflection...I tried doing f.GetDeclaringType i.e. (foreach field f in fields) but it returns A instead of int... and f.gettype() returns null Commented Jun 27, 2013 at 4:44
  • i will update question... Commented Jun 27, 2013 at 4:45
  • Side note: it looks like you are using "variable" as synonym of "field" (based on "variable or property in a class"). It is very unusual to call "field" this way - you may want to update title/question if indeed you talk about fields. Commented Jun 27, 2013 at 5:03

1 Answer 1

2

To get the property value of the field use the FieldInfo classes FieldType property

foreach (FieldInfo fieldInfo in typeof(A).GetFields(BindingFlags.Instance |
                               BindingFlags.Static |
                               BindingFlags.NonPublic |
                               BindingFlags.Public))
                {
                    Console.WriteLine(fieldInfo.FieldType.Name);
                }
Sign up to request clarification or add additional context in comments.

2 Comments

That returns back Nullable - assumming that instead of actually being int its int? but it doesnt tell me its an int though...
+1 as it answers the question as asked. @user2238556, please update your question with what your problem actually is. You may need to add handling for some structural types (like Nullable, Array, List<T> if you want to write more generic serialization).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.