自定义 class 实现接口 IDictionary<type, type> 可以是静态的?
Custom class implementing interface IDictionary<type, type> that could be static?
这是一个有点 Software Engineering 和 C# 的问题。在我看来,这个问题将最终解决所有关于 c# 可以做什么的问题,所以我先 post 在这里。
我有一个项目有多个 classes 需要访问同一个数据集。我为具有私有 Dictionary<byte[], MyCustomInterface>
实例的数据集创建了自定义 class。 class 实现了 IDictionary<byte[], MyCustomInterface>
。对于方法和属性,我只是包装了私有字典方法和属性。然后我添加了一些我自己的方法和属性来满足我的特定需求。
正如我所说,我需要访问自定义词典及其许多 classes 中的数据。我尝试将我的自定义 class 设置为静态但不能,因为它实现了接口。
我可以在后台创建一个数据库,但这将是一种重量级的解决方案。我以前做过这个,但最终需要大量维护。
还有哪些其他方法可以让我的所有 class 访问同一组 data/same class?我需要能够 serialize/deserialize 这个自定义数据集并将数据保存在文件中以供以后检索。我不想放弃使用接口,它们真的很方便。
public class EntityDictionary : IDictionary<byte[], IDispenseEntity>
{
private Dictionary<byte[], IDispenseEntity> _backing = new(new ByteArrayComparer());
public IDispenseEntity this[byte[] key]
{
get => _backing[key];
set => _backing[key] = value;
}
public ICollection<byte[]> Keys => _backing.Keys;
public ICollection<IDispenseEntity> Values => _backing.Values;
...
//My custom properties and methods
}
单例模式?
public class EntityDictionary : IDictionary<byte[], IDispenseEntity>
{
private EntityDictionary() {}
static EntityDictionary() {}
private static _instance = new EntityDictionary();
public static Instance { get { return _instance; }}
...
}
所以只有一个字典被所有用户共享,你不能创建自己的实例,并且被迫使用单个实例。
这是一个有点 Software Engineering 和 C# 的问题。在我看来,这个问题将最终解决所有关于 c# 可以做什么的问题,所以我先 post 在这里。
我有一个项目有多个 classes 需要访问同一个数据集。我为具有私有 Dictionary<byte[], MyCustomInterface>
实例的数据集创建了自定义 class。 class 实现了 IDictionary<byte[], MyCustomInterface>
。对于方法和属性,我只是包装了私有字典方法和属性。然后我添加了一些我自己的方法和属性来满足我的特定需求。
正如我所说,我需要访问自定义词典及其许多 classes 中的数据。我尝试将我的自定义 class 设置为静态但不能,因为它实现了接口。
我可以在后台创建一个数据库,但这将是一种重量级的解决方案。我以前做过这个,但最终需要大量维护。
还有哪些其他方法可以让我的所有 class 访问同一组 data/same class?我需要能够 serialize/deserialize 这个自定义数据集并将数据保存在文件中以供以后检索。我不想放弃使用接口,它们真的很方便。
public class EntityDictionary : IDictionary<byte[], IDispenseEntity>
{
private Dictionary<byte[], IDispenseEntity> _backing = new(new ByteArrayComparer());
public IDispenseEntity this[byte[] key]
{
get => _backing[key];
set => _backing[key] = value;
}
public ICollection<byte[]> Keys => _backing.Keys;
public ICollection<IDispenseEntity> Values => _backing.Values;
...
//My custom properties and methods
}
单例模式?
public class EntityDictionary : IDictionary<byte[], IDispenseEntity>
{
private EntityDictionary() {}
static EntityDictionary() {}
private static _instance = new EntityDictionary();
public static Instance { get { return _instance; }}
...
}
所以只有一个字典被所有用户共享,你不能创建自己的实例,并且被迫使用单个实例。