问题描述
public class ServerState
{
public static Action stateChanged;
private string currentMap;
public string CurrentMap
{
get { return currentMap; }
set
{
currentMap = value;
stateChanged?.Invoke();
}
}
}
我有一个包含数十个变量的类,每个变量都需要自己的属性,以便我可以调用操作。换句话说,我将重复上述代码数十次,并且我认为应该有更好的方法。
上面的代码有简写吗?
解决方法
这里有两个想法:
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace ConsoleApp1
{
public class ServerState
{
public static Action stateChanged;
private Dictionary<string,object> _values = new Dictionary<string,object>();
private void Set(object value,[CallerMemberName] string propertyName = null)
{
_values[propertyName] = value;
stateChanged?.Invoke();
}
private T Get<T>([CallerMemberName] string propertyName = null)
{
if (_values.TryGetValue(propertyName,out var v)) return (T)v;
throw new KeyNotFoundException(propertyName);
}
public string CurrentMap
{
get => Get<string>();
set => Set(value);
}
// You can make them in one line if you want
public string CurrentMap2 { get => Get<string>(); set => Set(value); }
}
public class ServerState2
{
public static Action stateChanged;
private string currentMap;
private void Set<T>(ref T property,T value)
{
property = value;
stateChanged?.Invoke();
}
public string CurrentMap
{
get => currentMap;
set => Set(ref currentMap,value);
}
}
}
如果您要使用多个事件,或者跟踪值是否实际更改,则可以扩展此想法:
public class ServerState
{
public static Action currentMapChanged;
public static Action currentMap2Changed;
private Dictionary<string,object>();
private void Set(object value,Action onChange,[CallerMemberName] string propertyName = null)
{
var previousValue = _values[propertyName];
// Check if value has changed
if (value != previousValue)
{
_values[propertyName] = value;
onChange?.Invoke();
}
}
private T Get<T>([CallerMemberName] string propertyName = null)
{
if (_values.TryGetValue(propertyName,out var v)) return (T)v;
throw new KeyNotFoundException(propertyName);
}
public string CurrentMap
{
get => Get<string>();
// Call `currentMapChanged` if value differs
set => Set(value,currentMapChanged);
}
// You can make them in one line if you want
// Call `currentMap2Changed` if value differs
public string CurrentMap2 { get => Get<string>(); set => Set(value,currentMap2Changed); }
}
此外,您应该研究使用event
关键字来阻止其他类触发此事件。还要注意静态事件,它们在静态上下文中拥有对侦听委托的引用,并且需要格外小心以避免内存泄漏。