none
get the propertyInfo.getvalue(object , new object[] {} ) ,reflection

    Question

  • Hi  ,  
    My  problem  is I have  a generic method  that must  filter  any  type  of data source bounded  to  a datagridview ,so  here each  the propertyname   and  and  the type of the   class   are unknown  until  the runtime .  here the code I have used  to  make   the  test 



    public partial class Form1 : Form
        {
            public PropertyData property { get; set; }
            public string FilterValue { get; set; }    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                IList<ItemDgv> items = new List<ItemDgv>();
                for (int i = 0; i < 10; i++)
                {
                  items.Add(new ItemDgv("item1" + i.ToString() , "item2"+i.ToString(),i));      
                }
                dataGridView1.DataSource =  items    ;
                dataGridView1.Columns.Add(new DataGridViewButtonColumn());             
    
                
            }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
                Filter((object)(dataGridView1.Rows[0].DataBoundItem as ItemDgv));   
            }
            private void Filter<T>(T t)
            {
                 //                Type tp = typeof(T);  
                string propName =  string.Empty    ;  
    
                foreach (PropertyInfo  prop1 in ((t.GetType()).GetProperties()))
                {
                     propName = prop1.Name;   
                     PropertyInfo prop = typeof(T).GetProperty(propName);
              
                object value1 = prop.GetValue(t,null);    //  here   nullReferenceException 
                object value2 = prop.GetValue(t, new object []{}); // here  TargetParameterCountException   
                object value3 = prop.GetValue(null  , null);  //  nullRefernceException
    
                                  
                }
               
    
     
                
            }
          
        }
        public class PropertyData
        {
            public string Description { get; set; }
            public string PropertyName { get; set; }
        }
        public class ItemDgv
        {
            private string _item = string.Empty;
            private string _item2 = string.Empty;
            private int _val2 = 0;   
           
       
            public ItemDgv( string prop1 ,  string  prop2 ,  int val1 )
            {
                _item = prop1;
                _item2 = prop2;
                _val2 = val1;  
                
    
            }
            public string Item1
            {
                get { return _item; }
                set { _item = value; }  
    
            }
    
            public string Item2
            {
                get { return _item2;   }
                set { _item2 = value; }   
            }
            public int itemInt
            {
                get { return _val2;   }   
            }
           
    
        }
    so can  any  one  tell  me how can  I  use this   method  to retrieve the  value of property 
    any  help  will  be very  appreciated 
    Thank's  in  advance   ...    
    A man's dreams are an index to his greatness
    Tuesday, August 04, 2009 1:02 PM

Answers

  • This may not be the cause of your problem, but why do you do this :

    foreach (PropertyInfo  prop1 in ((t.GetType()).GetProperties()))
                {
                     propName = prop1.Name;  
                     PropertyInfo prop = typeof(T).GetProperty(propName);

    You already have the PropertyInfo in prop1, so why get the Name and then get the PropertyInfo again using GetProperty? I suspect that for some reason the GetProperty call is returning null.
    http://blog.voidnish.com
    Tuesday, August 04, 2009 1:14 PM
  • You are doing things twice:

                foreach (PropertyInfo  prop1 in ((t.GetType()).GetProperties()))
                {
                     propName = prop1.Name;  
                     PropertyInfo prop = typeof(T).GetProperty(propName);

    First, you get all the properties (and thus the property info). Then, you retrieve the PropertyInfo of the property again. That is not necessary.

    The PropertyInfo class contains a lot of information. For example, you can get the value like this:

    // Get the type
    Type type = t.GetType();
    
    // Loop all properties of the type
    foreach (PropertyInfo propertyInfo in type.GetProperties())
    {
        // Now get the value
        object value = propertyInfo.GetValue(t, null);
    }
    


    However, if you want to be able to get all properties (for example, static properties, indexed properties, etc), you will have to add some additional checks to use the right GetValue implementation
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com
    • Proposed as answer by Geert van Horrik Wednesday, August 05, 2009 5:20 AM
    • Marked as answer by Bin-ze Zhao Friday, August 07, 2009 9:37 AM
    Tuesday, August 04, 2009 1:18 PM
  • your problem is solved.
    Acutally this line
     PropertyInfo prop = typeof(T).GetProperty(propName);

    is causing a problem. In this line prop is null hence you are getting exception in these 3 lines.

                object value1 = prop.GetValue(t,null);    //  here   nullReferenceException
                object value2 = prop.GetValue(t, new object []{}); // here  TargetParameterCountException  
                object value3 = prop.GetValue(null  , null);  //  nullRefernceException

    Solution: Instead of using
    PropertyInfo prop = typeof(T).GetProperty(propName);
             
                object value1 = prop.GetValue(t,null);    //  here   nullReferenceException
                object value2 = prop.GetValue(t, new object []{}); // here  TargetParameterCountException  
                object value3 = prop.GetValue(null  , null);  //  nullRefernceException

    use

      propName = prop1.Name;   
                            
                object value1 = prop1.GetValue(t,null);    //  item10 
                object value2 = prop1.GetValue(t, new object []{}); //item10   
                object value3 = prop1.GetValue(null  , null);  //  wrong syntax remove this line.

    hope this will help.

    TK
    • Marked as answer by Bin-ze Zhao Friday, August 07, 2009 9:35 AM
    Tuesday, August 04, 2009 7:11 PM

All replies

  • This may not be the cause of your problem, but why do you do this :

    foreach (PropertyInfo  prop1 in ((t.GetType()).GetProperties()))
                {
                     propName = prop1.Name;  
                     PropertyInfo prop = typeof(T).GetProperty(propName);

    You already have the PropertyInfo in prop1, so why get the Name and then get the PropertyInfo again using GetProperty? I suspect that for some reason the GetProperty call is returning null.
    http://blog.voidnish.com
    Tuesday, August 04, 2009 1:14 PM
  • You are doing things twice:

                foreach (PropertyInfo  prop1 in ((t.GetType()).GetProperties()))
                {
                     propName = prop1.Name;  
                     PropertyInfo prop = typeof(T).GetProperty(propName);

    First, you get all the properties (and thus the property info). Then, you retrieve the PropertyInfo of the property again. That is not necessary.

    The PropertyInfo class contains a lot of information. For example, you can get the value like this:

    // Get the type
    Type type = t.GetType();
    
    // Loop all properties of the type
    foreach (PropertyInfo propertyInfo in type.GetProperties())
    {
        // Now get the value
        object value = propertyInfo.GetValue(t, null);
    }
    


    However, if you want to be able to get all properties (for example, static properties, indexed properties, etc), you will have to add some additional checks to use the right GetValue implementation
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com
    • Proposed as answer by Geert van Horrik Wednesday, August 05, 2009 5:20 AM
    • Marked as answer by Bin-ze Zhao Friday, August 07, 2009 9:37 AM
    Tuesday, August 04, 2009 1:18 PM
  • thank you 
    but    the exception  came from  the reason  that  the  T it's  type and not an instance  so  will  show  me nullreference exception    thank's to all 


    A man's dreams are an index to his greatness
    Tuesday, August 04, 2009 1:30 PM
  • your problem is solved.
    Acutally this line
     PropertyInfo prop = typeof(T).GetProperty(propName);

    is causing a problem. In this line prop is null hence you are getting exception in these 3 lines.

                object value1 = prop.GetValue(t,null);    //  here   nullReferenceException
                object value2 = prop.GetValue(t, new object []{}); // here  TargetParameterCountException  
                object value3 = prop.GetValue(null  , null);  //  nullRefernceException

    Solution: Instead of using
    PropertyInfo prop = typeof(T).GetProperty(propName);
             
                object value1 = prop.GetValue(t,null);    //  here   nullReferenceException
                object value2 = prop.GetValue(t, new object []{}); // here  TargetParameterCountException  
                object value3 = prop.GetValue(null  , null);  //  nullRefernceException

    use

      propName = prop1.Name;   
                            
                object value1 = prop1.GetValue(t,null);    //  item10 
                object value2 = prop1.GetValue(t, new object []{}); //item10   
                object value3 = prop1.GetValue(null  , null);  //  wrong syntax remove this line.

    hope this will help.

    TK
    • Marked as answer by Bin-ze Zhao Friday, August 07, 2009 9:35 AM
    Tuesday, August 04, 2009 7:11 PM
  • You can also call events: I created a test dll that just has an Add method

    //Example

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFrom(@"C:\Documents and Settings\john.grove\Desktop\MyMathTest.dll");
                Type mathUtility = assembly.GetType("MyMathTest.MathUtility");
                Object theInstance = Activator.CreateInstance(mathUtility);
                Int32 result = (Int32)mathUtility.InvokeMember("Add", BindingFlags.InvokeMethod, null, theInstance, new object[] { 56, 26 });
                Console.WriteLine("Dynamically invoking MyMathTest.MathUtility Add method");
                Console.WriteLine("56 + 26 = {0}", result);
                Console.WriteLine("");

                // get all public static methods of MyMathTest.MathUtility type
                MethodInfo[] methodInfos = mathUtility.GetMethods(BindingFlags.Public | BindingFlags.Static);
                Console.WriteLine("All public/static methods in MyMathTest.MathUtility");
                Console.WriteLine("-------------------------------------------------");
                for (Int32 i = 0; i < methodInfos.Count(); i++ )
                    Console.WriteLine("{0}.) {1}", i + 1, methodInfos[i].Name);
                Console.ReadLine();
            }
        }
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, August 04, 2009 7:19 PM