Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不允许密封类作为泛型类型参数?_C#_.net_Generics - Fatal编程技术网

C# 为什么不允许密封类作为泛型类型参数?

C# 为什么不允许密封类作为泛型类型参数?,c#,.net,generics,C#,.net,Generics,我有: public abstract class DbManager<ConnectionType> where ConnectionType : class, IDbConnection, new() {...} a) 为什么限制将密封类用作泛型参数(!参数,而不是约束!)? b) 如何解决这个问题? c*)把那个班级封起来是个好主意吗 也许你有很多理由认为这个通用的数据库管理器不是一个好主意,但是,无论如何,我只想知道是否有可能在通用PARAM中处理这个密封类。谢谢

我有:

public abstract class DbManager<ConnectionType>
    where ConnectionType : class, IDbConnection, new() {...}
a) 为什么限制将密封类用作泛型参数(!参数,而不是约束!)?
b) 如何解决这个问题?
c*)把那个班级封起来是个好主意吗

也许你有很多理由认为这个通用的数据库管理器不是一个好主意,但是,无论如何,我只想知道是否有可能在通用PARAM中处理这个密封类。谢谢

PPS。错误消息:

“Microsoft.AnalysisServices.AdomdClient.AdomdConnection”必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法中的参数

另一条错误消息指定:

类型“Microsoft.AnalysisServices.AdomdClient.AdomdConnection” 不能在泛型类型或方法中用作类型参数 没有来自的装箱转换或类型参数转换 “Microsoft.AnalysisServices.AdomdClient.AdomdConnection”到 'System.Data.IDbConnection'


AdomdConnection
有一个无参数的公共构造函数。

我相信对密封类没有这样的限制。下面的代码编译得很好,因此可能还有其他问题

public abstract class DbManager<T> where T : class, IDbConnection, new()
{
}

public class ConcreteDbManagerUsingSealedClass : DbManager<MySealedConnection>
{
}

public sealed class MySealedConnection : IDbConnection
{
    public IDbTransaction BeginTransaction(IsolationLevel il)
    {
        throw new NotImplementedException();
    }

    public IDbTransaction BeginTransaction()
    {
        throw new NotImplementedException();
    }

    public void ChangeDatabase(string databaseName)
    {
        throw new NotImplementedException();
    }

    public void Close()
    {
        throw new NotImplementedException();
    }

    public string ConnectionString
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public int ConnectionTimeout
    {
        get { throw new NotImplementedException(); }
    }

    public IDbCommand CreateCommand()
    {
        throw new NotImplementedException();
    }

    public string Database
    {
        get { throw new NotImplementedException(); }
    }

    public void Open()
    {
        throw new NotImplementedException();
    }

    public ConnectionState State
    {
        get { throw new NotImplementedException(); }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}
公共抽象类DbManager,其中T:class,IDbConnection,new()
{
}
公共类ConcreteDbManagerUsingSealedClass:DbManager
{
}
公共密封类MySealedConnection:IDbConnection
{
公共IDBTransactionBeginTransaction(隔离级别il)
{
抛出新的NotImplementedException();
}
公共IDbTransaction BeginTransaction()
{
抛出新的NotImplementedException();
}
公共void ChangeDatabase(字符串databaseName)
{
抛出新的NotImplementedException();
}
公众假期结束()
{
抛出新的NotImplementedException();
}
公共字符串连接字符串
{
得到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
公共int连接超时
{
获取{抛出新的NotImplementedException();}
}
公共IDbCommand CreateCommand()
{
抛出新的NotImplementedException();
}
公共字符串数据库
{
获取{抛出新的NotImplementedException();}
}
公开作废
{
抛出新的NotImplementedException();
}
公共连接州
{
获取{抛出新的NotImplementedException();}
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}

您收到了什么错误消息?@Rawling:请看我的编辑是否可能在您的作用域中有另一个同名的类不符合条件?也许是另一个汇编版本?我无法在本地复制此内容。“
AdomdConnection
有一个无参数的公共构造函数。”请仔细检查,因为我不想认为编译器在撒谎。@Ken:请查看我的编辑。我编译了我拥有
AdomdConnection conn=newadomdconnection()的程序集我还使用
公共类ConcreteDBManagerSusingDomainConnectionClass:DbManager
进行编译。。。mistery…我还有另一个:类型“Microsoft.AnalysisServices.AdomdClient.AdomdConnection”不能用作泛型类型或方法中的类型参数。没有从“Microsoft.AnalysisServices.AdomdClient.AdomdConnection”到“System.Data.IDbConnection”的装箱转换或类型参数转换也基于此转换//程序集Microsoft.AnalysisServices.AdomdClient.dll,v2.0.50727“有没有可能你把.NET2和.NET4混合在一起,因此发生了一些奇怪的事情?”?。。。也许Adodm是基于.NET 2的IDbConnection构建的,而您的代码的其余部分是.NET 4?我不知道是怎么回事,但我应该在解决方案中向另一个项目(使用我的泛型类)添加对该程序集(AdomdClient)的引用,以使其正常工作。。。。谢谢啊,那时候你少了一些依赖。我有时会遇到类似的奇怪情况(如果我没记错的话)尝试使用(someDisposable){}执行某个操作,但我在项目引用中没有引用System.Core.dll或System.dll。。。或者类似的。
// Assembly Microsoft.AnalysisServices.AdomdClient.dll, v2.0.50727
using System;
using System.ComponentModel;
using System.Data;

namespace Microsoft.AnalysisServices.AdomdClient
{
    public sealed class AdomdConnection : Component, IDbConnection, 
                                                     IDisposable, ICloneable
    {
        public AdomdConnection();
public abstract class DbManager<T> where T : class, IDbConnection, new()
{
}

public class ConcreteDbManagerUsingSealedClass : DbManager<MySealedConnection>
{
}

public sealed class MySealedConnection : IDbConnection
{
    public IDbTransaction BeginTransaction(IsolationLevel il)
    {
        throw new NotImplementedException();
    }

    public IDbTransaction BeginTransaction()
    {
        throw new NotImplementedException();
    }

    public void ChangeDatabase(string databaseName)
    {
        throw new NotImplementedException();
    }

    public void Close()
    {
        throw new NotImplementedException();
    }

    public string ConnectionString
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public int ConnectionTimeout
    {
        get { throw new NotImplementedException(); }
    }

    public IDbCommand CreateCommand()
    {
        throw new NotImplementedException();
    }

    public string Database
    {
        get { throw new NotImplementedException(); }
    }

    public void Open()
    {
        throw new NotImplementedException();
    }

    public ConnectionState State
    {
        get { throw new NotImplementedException(); }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}