版权声明:本文为博主原创文章,欢迎各位转载,但须注明出处 https://blog.csdn.net/qq_34202873/article/details/86094269
C#/.NET 泛型+索引器搭建通用字典Dictionary
public enum Types
{
X,
Y,
Z,
W
}
public class DemoInfoMap<T>
{
private Dictionary<Types, T> demoMap = new Dictionary<Types, T>();
public DemoInfoMap()
{
this.demoMap.Add(Types.X, default(T));
this.demoMap.Add(Types.Y, default(T));
this.demoMap.Add(Types.Z, default(T));
this.demoMap.Add(Types.W, default(T));
}
//索引器参数需跟字典的key一样
public T this[Types axis]
{
get
{
return this.demoMap[axis];
}
set
{
this.demoMap[axis] = value;
}
}
public DemoInfoMap<UInt32> DemoInfo
{
get
{
var demoInfo = new DemoInfoMap<UInt32>();
demoInfo[Types.X] = 2;
return demoInfo;
}
}
}
调用
public DemoInfoMap<UInt32> DemoStatusInfo
{
get
{
var statusInfo = new DemoInfoMap<UInt32>();
statusInfo[Types.X] = 2;
return statusInfo;
}
}
public void DemoControl()
{
var index = DemoStatusInfo[Types.X];
}