none
About Reflection Methods

    Question

  • Hello,
    My a Class cant reach to my bussiness classes.. Methods of the class get Interface as parametter. so I cant use every property of the parameter. so to use the properties Im using reflection..

    obj.GetType().GetProperty("Name").GetValue(obj,null);

    to get the "Name" property Im using reflection...

    I want to know does it effect the performance ?
    Friday, May 13, 2005 9:46 PM

Answers

  • I think that this is a bad way to go about it for the following reasons:

    - It's not type safe, therefore if the property Name ever changes you are not going to find about it until runtime
    - Its a hell-of-a-lot slower than using interfaces
    - You class probably shouldn't known intimate details about the class passed in via an interface parameter, as it defeats the purpose of using a interface in the first place.

    You can do one of the following:

    - Expose more properties via the interface
    - Change the interface to use the object instead
    - Cast the interface parameter to the object of the properties you are trying to get

    The first option would be my most preferred.




    Friday, May 13, 2005 11:51 PM

All replies

  • Yes, but quite a bit.
    Try to below code.
    Direct access took 15 ms on my computer and reflection access took 6600 ms.


    class TestMe
    {
     const int TestCount = 1000000;
     private string name = "hello";
     public string Name { get { return this.name; } }

     /// <summary>
     /// The main entry point for the application.
     /// </summary>
     [STAThread]
     static void Main(string[] args)
     {

      TestMe test = new TestMe();

      long start_t = Environment.TickCount;

      for (int i = 0; i < TestCount; i++)
      {
       string temp = test.Name;
      }

      long end_t = Environment.TickCount;

      Console.WriteLine("Direct access took {0} ms.", end_t - start_t);

      start_t = Environment.TickCount;

      for (int i = 0; i < TestCount; i++)
      {
       string temp = (string)test.GetType().GetProperty("Name").GetValue(test, null);
      }

      end_t = Environment.TickCount;

      Console.WriteLine("Reflection access took {0} ms.", end_t - start_t);

     }
    }


     

    Friday, May 13, 2005 10:38 PM
  • I think that this is a bad way to go about it for the following reasons:

    - It's not type safe, therefore if the property Name ever changes you are not going to find about it until runtime
    - Its a hell-of-a-lot slower than using interfaces
    - You class probably shouldn't known intimate details about the class passed in via an interface parameter, as it defeats the purpose of using a interface in the first place.

    You can do one of the following:

    - Expose more properties via the interface
    - Change the interface to use the object instead
    - Cast the interface parameter to the object of the properties you are trying to get

    The first option would be my most preferred.




    Friday, May 13, 2005 11:51 PM