none
get runtime variable from abstract class RRS feed

Answers

  • Hi,

     

    I'm not sure what variable you want to pick up.

    As you get the properties by GetProperties you will get the class level variables by GetFields.

    For example

    Code Snippet

    TestDerived test = new TestDerived("This is a test");

    Type testType = test.GetType();

    FieldInfo[] fields = testType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);

    foreach (var field in fields)

    {

    Console.WriteLine("Field {0} of type {1} has value {2}", field.Name, field.FieldType.Name, field.GetValue(test));

    }

    Console.ReadKey();

     

     

    The important is the parameter you pass to the GetFields() method. In this case i want instance fields (no static ones) and they should be NonPublic (to catch the private ones). The value of the field is accessed by field.GetValue(test) where field is of type FieldInfo and test is the object which contains the field (this is you want to expect the variables of the derived class).

    Hope this helps.

    Philipp

    Monday, March 24, 2008 11:34 AM

All replies

  •  

    Thursday, March 20, 2008 6:51 AM
  • Can you post a short example of what you're trying to do?

    Thursday, March 20, 2008 7:11 AM
  •  

    Thursday, March 20, 2008 7:18 AM

  • Code Snippet

            private void PrintFields(object obj, Type baseType)
            {
                Type _T = obj.GetType();
                FieldInfo[] _Fields = _T.GetFields(BindingFlags.Instance);
                foreach (FieldInfo _Info in _Fields)
                {
                    if (_Info.DeclaringType == baseType)
                    {
                        object _Value = _Info.GetValue(obj);
                        Console.WriteLine("{0}={1}",
                            _Info.Name, _Value == null ? "NULL" : _Value
                            );
                    }
                }

            }

    Thursday, March 20, 2008 7:19 AM
  • It's still not clear what you are trying to do. Do you want to access a property of a derived class (like "_claimID") ?

     

    Please provide some more detailed information.
    Thursday, March 20, 2008 7:26 AM
  •  

    Thursday, March 20, 2008 7:41 AM
  •  

    Thursday, March 20, 2008 7:43 AM
  • I'm sorry, but I still don't understand. Can you post the pseudo-code for what you want to do? The code doesn't have to be correct, but please post the three classes you mentioned, plus some pseudo-code of what you want to do.

     

     

    Thursday, March 20, 2008 8:02 AM
  •  

    Thursday, March 20, 2008 8:22 AM
  •  

    Thursday, March 20, 2008 8:22 AM
  • Hi

     

    anyone can help me to fetch local variable from abstract class during runtime. i used propertyinfo[] to fetch property of the class. but my requirement is fetch local variable during runtime of the class.

     

    More Specific

     

    i have abstract class having only protected variable which is inherited by derived class having get set property of the abstract class variables. i have created instance of the derived class having all the properties of the derived class and all the variable of the abstract class.

    please find the class given below where i need this logic...

     

     

    public abstract class Parameter

    {

    protected string _claimIDField = default(string);

    public Void GetVariable()

    {

    // wrtie logic here to fetch local variable

    }

    }

     

     

    public class Inquiry : Parameter

    {

    public string _claimID

    {

    get{return this._claimIDField;}

    set{this._claimIDField = value;}

    }

    }

     

     

     

     

     

     

     

    Monday, March 24, 2008 7:01 AM
  • Hi,

     

    I'm not sure what variable you want to pick up.

    As you get the properties by GetProperties you will get the class level variables by GetFields.

    For example

    Code Snippet

    TestDerived test = new TestDerived("This is a test");

    Type testType = test.GetType();

    FieldInfo[] fields = testType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);

    foreach (var field in fields)

    {

    Console.WriteLine("Field {0} of type {1} has value {2}", field.Name, field.FieldType.Name, field.GetValue(test));

    }

    Console.ReadKey();

     

     

    The important is the parameter you pass to the GetFields() method. In this case i want instance fields (no static ones) and they should be NonPublic (to catch the private ones). The value of the field is accessed by field.GetValue(test) where field is of type FieldInfo and test is the object which contains the field (this is you want to expect the variables of the derived class).

    Hope this helps.

    Philipp

    Monday, March 24, 2008 11:34 AM