PropertyInfo.SetValue()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FourEyes

    PropertyInfo.SetValue()

    I've read that Reflection is slow and shouldn't be used when
    performance is a concern. In my case, performance is definitely a
    concern, so I was thinking that I could collect the PropertyInfo
    objects that I need at initialization, and then just call
    PropertyInfo.Se tValue() at runtime. Can anyone tell me 1) how
    expensive is the call to PropertyInfo.Se tValue() ? and 2) what is a
    better alternative ?

    I already have a hand-made solution where delegates are stored in a
    Dictionary but I am hoping that .NET has something that requires less
    code, ie, is auto-generated somehow.

    Thanks!

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: PropertyInfo.Se tValue()

    Well, using reflection is going to be pretty slow still. You are going
    to save some overhead on looking up the PropertyInfo instances, but most of
    the time is going to be wasted in calling SetValue.

    You can always measure the impact yourself using the StopWatch class in
    the System.Diagnost ics namespace.

    If the properties that you want to set the value of are public (or
    available to the code that will be setting the properties), then you can
    look into creating a dynamic assembly which will call the property directly,
    and assign it the value you want. This would be the fastest way to assign
    the value, but it will only work if you have access to the property from the
    calling code (if you are trying to call a private property, for example,
    then this wouldn't work).


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "FourEyes" <N.AhmedRobin@g mail.comwrote in message
    news:1194361341 .005408.51970@o 38g2000hse.goog legroups.com...
    I've read that Reflection is slow and shouldn't be used when
    performance is a concern. In my case, performance is definitely a
    concern, so I was thinking that I could collect the PropertyInfo
    objects that I need at initialization, and then just call
    PropertyInfo.Se tValue() at runtime. Can anyone tell me 1) how
    expensive is the call to PropertyInfo.Se tValue() ? and 2) what is a
    better alternative ?
    >
    I already have a hand-made solution where delegates are stored in a
    Dictionary but I am hoping that .NET has something that requires less
    code, ie, is auto-generated somehow.
    >
    Thanks!
    >

    Comment

    • =?Utf-8?B?QW5kcmV3IEZhdXN0?=

      #3
      RE: PropertyInfo.Se tValue()

      2) what is a better alternative ?

      Not sure if it's a better alternative for you, but there's a method that
      worked out well for me. I needed to add plugin capability to an application I
      built and the plugins had to be loaded via reflection at the start of the
      program for later use. What I did was built an abstract base class containing
      the members my app would call. The plugins then had to inherit from this
      class.

      When I instantiated the classes at runtime, I merely worked with them as the
      base type. This let me use reflection only to instantiate the classes. All
      the actual calls were handled directly via the base classes interface.

      You may be able to use an interface instead of burning the base class in
      your case. I needed some common functionality, though, so needed the base
      class.
      I am hoping that .NET has something that requires less code, ie, is auto generated somehow.
      There are code generators available that may suit your needs. Most of the
      generators I've seen are specific to database code, however, I think there
      are more generic ones available.

      You could also build your own. It wouldn't be too much trouble to build a
      program that uses reflection to query the class structures and generate code
      to do what you need explicitly.

      --
      Andrew Faust



      "FourEyes" wrote:
      I've read that Reflection is slow and shouldn't be used when
      performance is a concern. In my case, performance is definitely a
      concern, so I was thinking that I could collect the PropertyInfo
      objects that I need at initialization, and then just call
      PropertyInfo.Se tValue() at runtime. Can anyone tell me 1) how
      expensive is the call to PropertyInfo.Se tValue() ? and 2) what is a
      better alternative ?
      >
      I already have a hand-made solution where delegates are stored in a
      Dictionary but I am hoping that .NET has something that requires less
      code, ie, is auto-generated somehow.
      >
      Thanks!
      >
      >

      Comment

      • Norapinephrine

        #4
        Re: PropertyInfo.Se tValue()

        On 6 nov, 10:41, "Nicholas Paldino [.NET/C# MVP]"
        <m...@spam.guar d.caspershouse. comwrote:
        Well, using reflection is going to be pretty slow still. You are going
        to save some overhead on looking up the PropertyInfo instances, but most of
        the time is going to be wasted in calling SetValue.
        >
        You can always measure the impact yourself using the StopWatch class in
        the System.Diagnost ics namespace.
        >
        If the properties that you want to set the value of are public (or
        available to the code that will be setting the properties), then you can
        look into creating a dynamic assembly which will call the property directly,
        and assign it the value you want. This would be the fastest way to assign
        the value, but it will only work if you have access to the property from the
        calling code (if you are trying to call a private property, for example,
        then this wouldn't work).
        >
        --
        - Nicholas Paldino [.NET/C# MVP]
        - m...@spam.guard .caspershouse.c om
        >
        "FourEyes" <N.AhmedRo...@g mail.comwrote in message
        >
        news:1194361341 .005408.51970@o 38g2000hse.goog legroups.com...
        >
        >
        >
        I've read that Reflection is slow and shouldn't be used when
        performance is a concern. In my case, performance is definitely a
        concern, so I was thinking that I could collect the PropertyInfo
        objects that I need at initialization, and then just call
        PropertyInfo.Se tValue() at runtime. Can anyone tell me 1) how
        expensive is the call to PropertyInfo.Se tValue() ? and 2) what is a
        better alternative ?
        >
        I already have a hand-made solution where delegates are stored in a
        Dictionary but I am hoping that .NET has something that requires less
        code, ie, is auto-generated somehow.
        >
        Thanks!- Masquer le texte des messages précédents -
        >
        - Afficher le texte des messages précédents -
        Yeah, I haven't got around to timing the SetValue calls yet. I am
        rather hesitant to create a dynamic assembly for this solution because
        it sounds complicated. I need to be able to easily add classes to the
        solution, so I don't want to have anything complicated involved in the
        build process or in the code.

        However, the solution that I am trying out right now is also more
        complicated than I like, because it requires the developers to create
        delegates to set/get each field and then to store these into a
        dictionary.

        Do you think that using dynamic assemblies would be simpler and re-
        usable than my method of handwriting delegates for each property and
        manually stuffing them into a Dictionary ?

        Thanks again!
        N

        Comment

        • Norapinephrine

          #5
          Re: PropertyInfo.Se tValue()

          Hmmm... I would like to do something like
          >>>
          ClassA objA = new ClassA();

          foreach( KeyValuePair<pr opertyName,prop ertyValuein
          newPropertyValu es )
          {
          objA.SetPropert y(propertyName, propertyValue);
          }
          >>>
          How would I use a dynamic assembly to do something like that ?

          Thanks again,
          N


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: PropertyInfo.Se tValue()

            On Nov 6, 3:02 pm, FourEyes <N.AhmedRo...@g mail.comwrote:
            I've read that Reflection is slow and shouldn't be used when
            performance is a concern. In my case, performance is definitely a
            concern
            To what extent? Have you got some concrete goals in mind? Using
            reflection is slower than not using reflection, but that doesn't mean
            it's necessarily *too* slow.

            Unless you've got a test bed which measures performance and some
            targets, it will be hard to know when you've got a solution which is
            deemed acceptable.

            Jon

            Comment

            • Norapinephrine

              #7
              Re: PropertyInfo.Se tValue()

              On Nov 7, 9:13 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
              On Nov 6, 3:02 pm, FourEyes <N.AhmedRo...@g mail.comwrote:
              >
              I've read that Reflection is slow and shouldn't be used when
              performance is a concern. In my case, performance is definitely a
              concern
              >
              To what extent? Have you got some concrete goals in mind? Using
              reflection is slower than not using reflection, but that doesn't mean
              it's necessarily *too* slow.
              >
              Unless you've got a test bed which measures performance and some
              targets, it will be hard to know when you've got a solution which is
              deemed acceptable.
              >
              Jon
              Jon,

              We have an old version of the product which is taking a lot of time to
              complete, for various reasons. I can see now that for the case of
              creating 66K objects with about 20 properties that need to be set, it
              is quite slow. I will of course time the process with and without
              using PropertyInfo.Se tValue(), but I would like to get better
              performance without the maintenance cost of a complex solution.

              Thanks,
              Nora

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: PropertyInfo.Se tValue()

                Norapinephrine <N.AhmedRobin@g mail.comwrote:
                We have an old version of the product which is taking a lot of time to
                complete, for various reasons. I can see now that for the case of
                creating 66K objects with about 20 properties that need to be set, it
                is quite slow. I will of course time the process with and without
                using PropertyInfo.Se tValue(), but I would like to get better
                performance without the maintenance cost of a complex solution.
                How slow is it? On my laptop, I can do that in just over 7 seconds
                (code below - I've used C# 3 for automatic properties, but that's all).

                How often are you doing this?

                Using PropertyDescrip tor instead of PropertyInfo gives me about the
                same performance.

                If this isn't good enough, see whether Marc Gravell's
                "HyperDescripto r" code helps you:





                using System;
                using System.Diagnost ics;
                using System.Reflecti on;
                using System.Collecti ons.Generic;

                public class LotsOfPropertie s
                {
                public string Foo0 { get; set; }
                public string Foo1 { get; set; }
                public string Foo2 { get; set; }
                public string Foo3 { get; set; }
                public string Foo4 { get; set; }
                public string Foo5 { get; set; }
                public string Foo6 { get; set; }
                public string Foo7 { get; set; }
                public string Foo8 { get; set; }
                public string Foo9 { get; set; }
                public string Foo10 { get; set; }
                public string Foo11 { get; set; }
                public string Foo12 { get; set; }
                public string Foo13 { get; set; }
                public string Foo14 { get; set; }
                public string Foo15 { get; set; }
                public string Foo16 { get; set; }
                public string Foo17 { get; set; }
                public string Foo18 { get; set; }
                public string Foo19 { get; set; }
                }

                public class Test
                {
                static void Main()
                {
                List<PropertyIn foproperties = new List<PropertyIn fo>();
                for (int i=0; i < 20; i++)
                {
                properties.Add( typeof(LotsOfPr operties).
                GetProperty("Fo o"+i));
                }

                Stopwatch sw = Stopwatch.Start New();
                for (int i=0; i < 66000; i++)
                {
                object o = Activator.Creat eInstance
                (typeof(LotsOfP roperties));
                foreach (PropertyInfo prop in properties)
                {
                prop.SetValue(o , "Value", null);
                }
                }
                sw.Stop();
                Console.WriteLi ne (sw.ElapsedMill iseconds);
                }
                }

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Norapinephrine

                  #9
                  Re: PropertyInfo.Se tValue()

                  On Nov 8, 2:40 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                  Norapinephrine <N.AhmedRo...@g mail.comwrote:
                  We have an old version of the product which is taking a lot of time to
                  complete, for various reasons. I can see now that for the case of
                  creating 66K objects with about 20 properties that need to be set, it
                  is quite slow. I will of course time the process with and without
                  using PropertyInfo.Se tValue(), but I would like to get better
                  performance without the maintenance cost of a complex solution.
                  >
                  How slow is it? On my laptop, I can do that in just over 7 seconds
                  (code below - I've used C# 3 for automatic properties, but that's all).
                  >
                  How often are you doing this?
                  >
                  Using PropertyDescrip tor instead of PropertyInfo gives me about the
                  same performance.
                  >
                  If this isn't good enough, see whether Marc Gravell's
                  "HyperDescripto r" code helps you:
                  >

                  >
                  using System;
                  using System.Diagnost ics;
                  using System.Reflecti on;
                  using System.Collecti ons.Generic;
                  >
                  public class LotsOfPropertie s
                  {
                  public string Foo0 { get; set; }
                  public string Foo1 { get; set; }
                  public string Foo2 { get; set; }
                  public string Foo3 { get; set; }
                  public string Foo4 { get; set; }
                  public string Foo5 { get; set; }
                  public string Foo6 { get; set; }
                  public string Foo7 { get; set; }
                  public string Foo8 { get; set; }
                  public string Foo9 { get; set; }
                  public string Foo10 { get; set; }
                  public string Foo11 { get; set; }
                  public string Foo12 { get; set; }
                  public string Foo13 { get; set; }
                  public string Foo14 { get; set; }
                  public string Foo15 { get; set; }
                  public string Foo16 { get; set; }
                  public string Foo17 { get; set; }
                  public string Foo18 { get; set; }
                  public string Foo19 { get; set; }
                  >
                  }
                  >
                  public class Test
                  {
                  static void Main()
                  {
                  List<PropertyIn foproperties = new List<PropertyIn fo>();
                  for (int i=0; i < 20; i++)
                  {
                  properties.Add( typeof(LotsOfPr operties).
                  GetProperty("Fo o"+i));
                  }
                  >
                  Stopwatch sw = Stopwatch.Start New();
                  for (int i=0; i < 66000; i++)
                  {
                  object o = Activator.Creat eInstance
                  (typeof(LotsOfP roperties));
                  foreach (PropertyInfo prop in properties)
                  {
                  prop.SetValue(o , "Value", null);
                  }
                  }
                  sw.Stop();
                  Console.WriteLi ne (sw.ElapsedMill iseconds);
                  }
                  >
                  }
                  >
                  --
                  Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too
                  Jon, I hadn't got around to running the stopwatch test yet and your
                  results make it look like I don't have to worry about the time to run
                  a PropertyInfo.Se tValue() on a given object/value pair. So I'm
                  guessing that the code is taking too long probably because it is
                  looking up the property info each time and because the algorithms in
                  the set properties long.

                  This is great to know. I'll keep a tab on that link you provided too,
                  just in case I need it for something else.

                  Thanks a lot, Jon!
                  Nora


                  Comment

                  • Marc Gravell

                    #10
                    Re: PropertyInfo.Se tValue()

                    probably because it is looking up the property info each time

                    Absolutely; when I was looking into this area (for a flexibile data-
                    export process), I found that pre-obtaining the getter was a
                    significant saving by itself (regardless of whether that is a
                    PropertyInfo or a sneaky PropertyDescrip tor) - so what I did was to
                    build a PropertyDescrip tor-array and pre-fill it with the getters that
                    I needed - then my main output code was essentially:

                    foreach(row in data) {
                    startRow();
                    foreach(prop in propsWanted) {
                    writeValue(); // from prop and row
                    }
                    endRow();
                    }

                    Depending on your data, this might be enough... but then, since it
                    only takes one line of code to swap a reflection-based descriptor for
                    a dynamic IL-based descriptor, it is trivial to try it before and
                    after.

                    Marc

                    Comment

                    Working...