为什么我不能继承

时间:2014-12-29 06:12:51

标签: c# .net

我尝试将PersistentDictionary子类化,但编译器标记 base(storage_key)并说:

Error   1   'object' does not contain a constructor that takes 1 arguments  

这是我的代码:

public class MyPersistentDictionary<TKey, TValue> : PersistentDictionary<TKey, TValue> where TKey : IComparable<TKey>
    {
        private string storage_key;

        public MyPersistentDictionary(string storage_key):base(storage_key)
        {
            // TODO: Complete member initialization

            this.storage_key = storage_key;
        }
    }

我确信 PersistentDictionary 有一个带有一个字符串的构造函数: https://managedesent.codeplex.com/SourceControl/latest#EsentCollections/PersistentDictionary.cs

1 个答案:

答案 0 :(得分:5)

您尝试子类化的typesealed,因此没有类型可以继承。

public sealed partial class PersistentDictionary<TKey, TValue> : 
    IDictionary<TKey, TValue>, IDisposable
    where TKey : IComparable<TKey>

您当然可以通过将实例作为参数解决此问题。

public sealed class PersistentCache<TKey, TValue> : IDictionary<TKey, TValue>
{
    private readonly PersistentDictionary<TKey, TValue> _backingInstance;

    public PersistentCache(PersistentDictionary<TKey, TValue> backingInstance)
    {
        _backingInstance = backingInstance;    
    }

    // implement IDictionary<TKey, TValue>
}