Two questions about properties in C #

I have two simple questions based on what I am extending an existing class ...

1) I implement some custom properties, and I would like that whenever their values ​​change, a specific (without parameters) function is called. Of course, if there was no need to call the function, I could use the traditional syntax { get; set; }without requiring an additional variable. However, even if the only thing I modify in the setaccessor is to call another function, I have to declare a private variable so that I can define accessors myself getand set... Isn’t there an easier way to do this without declaring so many variables? Is there something like a gerenal property change event ?

2) Since I am overriding the .NET user control, I would like to change the category in which some basic properties appear. For now and just using the syntax [Category("")], I have to declare these properties as newwell as implement getand setreferring to the basic properties of the class. Isn't there an easy way to do this?

+3
source share
5 answers

, INotifyPropertyChanged. , . WPF. , , . (PropertyChanged.BeginInvoke()), , ...

using System;
using System.ComponentModel;

public class Foo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }
}
+1

# , AOP " " , - PostSharp . PostSharp IL , , Windsor .

AOP.

+1

, , - , Castle, , , .

0

1) There are no property change events. If you insist on not creating additional variables, you could go along the AOP route . You should be satisfied without AOP if there are no countless properties you are dealing with, in which case you could choose to generate code (perhaps write a python / ruby ​​script to generate code or use a generic code generator)

2) I do not think that you can change the pre-runtime properties (in Visual Studio) without generating code.

0
source

Maybe I'm here, and something like this for # 1:

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

namespace EraseMe2
{
class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.MyProperty = 100;
        t.MyProperty = 500;
    }
}


class Test
{
    public int set(ref int prop, int val)
    {
        prop = val;
        Console.WriteLine(String.Format("{0} changed to {1}", prop.GetType().Name, val.ToString()));
        return val;
    }

    private int _myProperty;
    public int MyProperty
    {
        get { return _myProperty; }
        set
        {
            _myProperty = set(ref _myProperty, value);
        }
    }
}

}
0
source

Source: https://habr.com/ru/post/1792543/


All Articles