Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. SetProperty!!!

SetProperty!!!

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpcomgraphicsdesigngame-dev
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    public static class Extensions
    {
    public static void SetProperty(this object domainEntity, string propertyName, object value)
    {
    var t = domainEntity.GetType();
    var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
    p.SetValue(domainEntity, value);
    }
    public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
    {
    var e = (MemberExpression)getP.Body;
    SetProperty(domainEntity, e.Member.Name, value);
    }
    }

    This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

    My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

    B D P B N 6 Replies Last reply
    0
    • S Super Lloyd

      public static class Extensions
      {
      public static void SetProperty(this object domainEntity, string propertyName, object value)
      {
      var t = domainEntity.GetType();
      var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
      p.SetValue(domainEntity, value);
      }
      public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
      {
      var e = (MemberExpression)getP.Body;
      SetProperty(domainEntity, e.Member.Name, value);
      }
      }

      This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

      My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      At least, static properties are still safe.

      1 Reply Last reply
      0
      • S Super Lloyd

        public static class Extensions
        {
        public static void SetProperty(this object domainEntity, string propertyName, object value)
        {
        var t = domainEntity.GetType();
        var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
        p.SetValue(domainEntity, value);
        }
        public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
        {
        var e = (MemberExpression)getP.Body;
        SetProperty(domainEntity, e.Member.Name, value);
        }
        }

        This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

        My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

        D Offline
        D Offline
        Duncan Edwards Jones
        wrote on last edited by
        #3

        Not only that - because you have to refer to the property by name you also break compile time type-checking.

        Richard DeemingR 1 Reply Last reply
        0
        • D Duncan Edwards Jones

          Not only that - because you have to refer to the property by name you also break compile time type-checking.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          I guess that's where the second overload comes in:

          codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);

          It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          D S N 3 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            I guess that's where the second overload comes in:

            codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);

            It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            D Offline
            D Offline
            Duncan Edwards Jones
            wrote on last edited by
            #5

            Yeah - Ironically the non type safe overload shouldn't be public :-)

            1 Reply Last reply
            0
            • S Super Lloyd

              public static class Extensions
              {
              public static void SetProperty(this object domainEntity, string propertyName, object value)
              {
              var t = domainEntity.GetType();
              var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
              p.SetValue(domainEntity, value);
              }
              public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
              {
              var e = (MemberExpression)getP.Body;
              SetProperty(domainEntity, e.Member.Name, value);
              }
              }

              This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

              My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Dagnabit, I keep hitting upvote, but it only registered once. :sigh: That's right up there with the "Singleton" articles that get posted here that require the target class to have a private constructor and then use Reflection to get it. :doh:

              You'll never get very far if all you do is follow instructions.

              S 1 Reply Last reply
              0
              • S Super Lloyd

                public static class Extensions
                {
                public static void SetProperty(this object domainEntity, string propertyName, object value)
                {
                var t = domainEntity.GetType();
                var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                p.SetValue(domainEntity, value);
                }
                public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
                {
                var e = (MemberExpression)getP.Body;
                SetProperty(domainEntity, e.Member.Name, value);
                }
                }

                This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

                My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                B Offline
                B Offline
                Brisingr Aerowing
                wrote on last edited by
                #7

                :doh: :wtf:

                What do you get when you cross a joke with a rhetorical question?

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  I guess that's where the second overload comes in:

                  codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);

                  It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  I created this second overload 2 days ago!! :laugh:

                  My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Dagnabit, I keep hitting upvote, but it only registered once. :sigh: That's right up there with the "Singleton" articles that get posted here that require the target class to have a private constructor and then use Reflection to get it. :doh:

                    You'll never get very far if all you do is follow instructions.

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #9

                    That's just as bad, indeed!!! :laugh:

                    My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                    1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      I guess that's where the second overload comes in:

                      codeReview.SetProperty(review => review.Status, Status.WhatTheElephant);

                      It's not fast, pretty, or even remotely sensible, but at least it's strongly typed and the property name is checked by the compiler.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      N Offline
                      N Offline
                      Nicolas Dorier
                      wrote on last edited by
                      #10

                      well, you can't use this line of code outside of review.GetType() class... Status is private ;)

                      1 Reply Last reply
                      0
                      • S Super Lloyd

                        public static class Extensions
                        {
                        public static void SetProperty(this object domainEntity, string propertyName, object value)
                        {
                        var t = domainEntity.GetType();
                        var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                        p.SetValue(domainEntity, value);
                        }
                        public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
                        {
                        var e = (MemberExpression)getP.Body;
                        SetProperty(domainEntity, e.Member.Name, value);
                        }
                        }

                        This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

                        My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                        N Offline
                        N Offline
                        Nicolas Dorier
                        wrote on last edited by
                        #11

                        It remind me when I wrote

                        static bool Is(this T obj);

                        with reflection... Then a friend of mind said : WTF have you written that ? instead of "obj is T". The worse of it is that I had no idea why I did something so stupid... wanted to blame somebody else, but the source control history pointed at me. :D

                        S 1 Reply Last reply
                        0
                        • N Nicolas Dorier

                          It remind me when I wrote

                          static bool Is(this T obj);

                          with reflection... Then a friend of mind said : WTF have you written that ? instead of "obj is T". The worse of it is that I had no idea why I did something so stupid... wanted to blame somebody else, but the source control history pointed at me. :D

                          S Offline
                          S Offline
                          Super Lloyd
                          wrote on last edited by
                          #12

                          Did you mean static bool Is<T>(this **object** obj);? Because T obj is... always... T as far as I can tell! :rolleyes: :laugh: Good one anyway! :-D

                          My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                          N 1 Reply Last reply
                          0
                          • S Super Lloyd

                            Did you mean static bool Is<T>(this **object** obj);? Because T obj is... always... T as far as I can tell! :rolleyes: :laugh: Good one anyway! :-D

                            My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                            N Offline
                            N Offline
                            Nicolas Dorier
                            wrote on last edited by
                            #13

                            yes it was this object good point ;)

                            1 Reply Last reply
                            0
                            • S Super Lloyd

                              public static class Extensions
                              {
                              public static void SetProperty(this object domainEntity, string propertyName, object value)
                              {
                              var t = domainEntity.GetType();
                              var p = t.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                              p.SetValue(domainEntity, value);
                              }
                              public static void SetProperty<T, TProp>(this T domainEntity, Expression<Func<T, TProp>> getP, TProp value)
                              {
                              var e = (MemberExpression)getP.Body;
                              SetProperty(domainEntity, e.Member.Name, value);
                              }
                              }

                              This piece of code has a fantastic history that is not obvious... Why is that you might ask? Well.. the single architect of our application decided that we were going to use Domain Driven Design (DDD). He also decided that all domain object property will have private setters. This being, as you can imagine, inconvenient, we got large use of this method (which he also wrote) to set private properties whenever needed! Brilliant! Let's use private property for safety! And let's use this method to ignore private! Genius! :suss: :-\ :-D

                              My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                              T Offline
                              T Offline
                              Tom Clement
                              wrote on last edited by
                              #14

                              I can imagine a use for something like this when implementing Undo/Redo.

                              Tom Clement articles[^]

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups