c# INotifyPropertyChanged

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <div id="app">
        <textarea v-model="input"></textarea>
        <button @click="submit">submit</button>
        <div contenteditable="true">
            <pre v-for="item in items">
                protected {{ item.type }} _{{ item.name }};
                public {{ item.type }} {{ item.name }}
                {
                    get { return this._{{ item.name }}; }
                    set
                    {
                        SetField(ref this._{{ item.name }}, value);
                    }
                }
            </pre>
        </div>
    </div>

    <script src="http://cdn.bootcss.com/vue/2.1.6/vue.min.js"></script>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    items: [],
                    input: null,
                }
            },
            methods: {
                submit() {
                    let lines = this.input
                        .split(/
/)
                        .map(t => t.trim().split(' '))
                        .filter(t => t.length > 6)

                    this.items = []

                    lines.forEach(arr => {
                        this.items.push({
                            name: arr[2],
                            type: arr[1],
                        })
                    })
                }
            }
        })
    </script>
</body>
</html>

#region INotifyPropertyChanged

protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
        handler(this, e);
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    //C# 6 null-safe operator. No need to check for event listeners
    //If there are no listeners, this will be a noop
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

// C# 5 - CallMemberName means we don't need to pass the property's name
protected bool SetField<T>(ref T field, T value,
[CallerMemberName] string propertyName = null)
{
    if (EqualityComparer<T>.Default.Equals(field, value))
        return false;
    field = value;
    OnPropertyChanged(propertyName);
    return true;
}

#endregion
原文地址:https://www.cnblogs.com/ChobitsSP/p/7452217.html