Is there a way to intercept setters and getters in C#?

前端 未结 11 1225
旧时难觅i
旧时难觅i 2021-01-03 18:15

In both Ruby and PHP (and I guess other languages as well) there are some utility methods that are called whenever a property is set. ( *instance_variable_set*

相关标签:
11条回答
  • 2021-01-03 18:55

    It is possible to do directly in the property body itself, but then you need to use a proper backing field instead of auto-implemented properties.

    private string firstName;
    
    public string FirstName 
    { 
      get { return firstName;} 
      set 
      {
        if(check(value))
        {
           firstName = value;
        }
      } 
    }
    

    Even with auto-implemented properties you get a backing field - this is generated by the compiler and you don't have direct access to it.


    Edit:

    Seeing as you don't want a backing field, you have other options - using an AOP tool such as PostSharp could help with that.

    0 讨论(0)
  • 2021-01-03 18:56

    Yes, of course...

    In your example you are using automatic properties, without a backing field.... You just need to create a backing field for your property, and then you can do what you want in the setter and getter.

    example:

    private string firstName;
    
    public string FirstName
    {
     get { return firstName; }
    
     set { doMethod(); firstName = value;}
    }
    
    0 讨论(0)
  • 2021-01-03 19:02

    I know this has been properly answered but I'll include an example to show you the syntax to achieve what you want:

    public class Person
    {
        private 
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                // see how we can call a method below? or any code for that matter..
                _firstName = SanitizeName(value);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 19:03

    You will have to write the properties in full to achieve this.

    0 讨论(0)
  • 2021-01-03 19:09

    As far as I know, you have to use a backing field and put the call to the other method inside the setter thusly:

    public class Person {
        private string firstName;
        private string lastName;
    
        public string FirstName {
            set { 
                DoSomeStuff();
                firstName = value;
            }
            get { return firstName; }
        }
    
        public string LastName { 
            set {
                DoSomeStuff();
                lastName = value;
            }
            get { return lastName; }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 19:12

    You cant use automatic properties. You would have to dinfe the property out the old fashion way with a backing field and call the method manually.

    public class Person
    {
        private string _FirstName;
        public string FirstName 
        { 
            get
            {
                return _FirstName;
            }
            set
            {
                SomeMethod();
                _FirstName = value;
            }
        }
        private void SomeMethod()
        {
            //do something
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题