Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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语言中带泛型的函数声明#_C#_Generics - Fatal编程技术网

C# C语言中带泛型的函数声明#

C# C语言中带泛型的函数声明#,c#,generics,C#,Generics,我有一个这样声明的函数: public static string MultiWhereToString(List<WhereCondition<T>> whereConditions) private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>(); public class WhereAndCondition<T>

我有一个这样声明的函数:

public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
public class WhereAndCondition<T> : WhereCondition<T>, IConditional where T : DatabaseObject
wherendcondition
WhereCondition
的子类。声明如下:

public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
public class WhereAndCondition<T> : WhereCondition<T>, IConditional where T : DatabaseObject
公共类wherendcondition:WhereCondition,IConditional where T:DatabaseObject
我的问题是,如果我尝试执行以下代码:

private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
MultiWhereToString(whereAnd);
private List wherend=new List();
多wheretoString(wherend);
我得到以下错误:

Error 3 Argument 1: cannot convert from 'System.Collections.Generic.List<BrainStorm.WhereAndCondition<T>>' to 'System.Collections.Generic.List<BrainStorm.WhereCondition<T>>'
错误3参数1:无法从“System.Collections.Generic.List”转换为“System.Collections.Generic.List”
你知道为什么吗?我认为这与
WhereCondition
类的泛型有关。

给定:

class A {}
class A : B {}
List
的对象不是
List
的实例。因此,您不能将
列表
强制转换为
列表
。您可以使用:

MultiWhereToString(whereAnd.OfType<WhereCondition>().ToList());
MultiWhereToString(wherend.OfType().ToList());
(可能还有一种解决方案涉及到
in
out
方差注释,但我对它们不是很熟悉。)

给出:

class A {}
class A : B {}
List
的对象不是
List
的实例。因此,您不能将
列表
强制转换为
列表
。您可以使用:

MultiWhereToString(whereAnd.OfType<WhereCondition>().ToList());
MultiWhereToString(wherend.OfType().ToList());

(可能还有一种解决方案涉及
in
out
方差注释,但我对它们不是很熟悉。)

您的函数被定义为获取WhereCondition列表,但您正在传递wherendcondition列表:

MultiWhereToString(列出whereConditions)

private List wherend=new List();
多wheretoString(wherend);

列表差异在.NET 4中受限制。请参阅。

您的函数被定义为获取WhereCondition列表,但您正在向其传递wherendcondition列表:

MultiWhereToString(列出whereConditions)

private List wherend=new List();
多wheretoString(wherend);

列表差异在.NET 4中受限制。请参阅。

泛型必须在编译时明确知道,因为它们是生成的

为什么不使用:

private List<WhereCondition<T>> whereAnd = new List<WhereCondition<T>>();
private List wherend=new List();

因此,您仍然可以将
wherendcondition
对象添加到
wherendcondition

泛型必须在编译时明确知道,因为它们是生成的

为什么不使用:

private List<WhereCondition<T>> whereAnd = new List<WhereCondition<T>>();
private List wherend=new List();

因此,您仍然可以将
wherendcondition
对象添加到
wherendcondition

中。您可以将
MultiWhereToString
方法中的整个
WhereCondition
替换为另一个限制为
WhereCondition
的泛型类型

替换:

public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
公共静态字符串MultiWhereToString(列出whereConditions)
与:

public static string MultiWhereToString<TType>(List<TType> whereConditions) where TType: WhereCondition<T>
公共静态字符串MultiWhereToString(列出whereConditions),其中TType:WhereCondition
或者改变:

private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
private List wherend=new List();
致:

private List wherend=new List();

让遗产为你照顾剩下的

您可以用另一个限制为
WhereCondition
的泛型类型替换
MultiWhereToString
方法中的整个
WhereCondition

替换:

public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
公共静态字符串MultiWhereToString(列出whereConditions)
与:

public static string MultiWhereToString<TType>(List<TType> whereConditions) where TType: WhereCondition<T>
公共静态字符串MultiWhereToString(列出whereConditions),其中TType:WhereCondition
或者改变:

private List<WhereAndCondition<T>> whereAnd = new List<WhereAndCondition<T>>();
private List wherend=new List();
致:

private List wherend=new List();

让遗产为你照顾剩下的

这似乎是一个协方差/反方差问题

简化为:

    public class WhereCondition
    {
    }

    public class WhereAndCondition : WhereCondition
    {
    }

    public class blah
    {
        public static void Blah()
        {
            List<WhereAndCondition> whereAnd = new List<WhereAndCondition>();
            MultiWhereToString(whereAnd);
        }

        public static string MultiWhereToString(List<WhereCondition> whereConditions)
        {
            return null;
        }
    }
公共类条件
{
}
公共类wherendcondition:WhereCondition
{
}
公开课之类的
{
公共静态无效Blah()
{
List wherend=新列表();
多wheretoString(wherend);
}
公共静态字符串MultiWhereToString(列出whereConditions)
{
返回null;
}
}
它不起作用,因为无法将wherendconditions列表转换为WhereConditions列表:

想象一下。你有一张长颈鹿的名单,方法是要一张动物的名单

在不知道它们将如何处理列表中的动物(如尝试添加马)的情况下,这些类型是不兼容的,但如果您将其更改为以下内容:

        public static string MultiWhereToString(IEnumerable<WhereCondition> whereConditions)
        {
            return null;
        }
公共静态字符串MultiWhereToString(IEnumerable whereConditions)
{
返回null;
}

然后方差就可以生效,并给出你想要的东西。

这似乎是一个协方差/反方差问题

简化为:

    public class WhereCondition
    {
    }

    public class WhereAndCondition : WhereCondition
    {
    }

    public class blah
    {
        public static void Blah()
        {
            List<WhereAndCondition> whereAnd = new List<WhereAndCondition>();
            MultiWhereToString(whereAnd);
        }

        public static string MultiWhereToString(List<WhereCondition> whereConditions)
        {
            return null;
        }
    }
公共类条件
{
}
公共类wherendcondition:WhereCondition
{
}
公开课之类的
{
公共静态无效Blah()
{
List wherend=新列表();
多wheretoString(wherend);
}
公共静态字符串MultiWhereToString(列出whereConditions)
{
返回null;
}
}
它不起作用,因为无法将wherendconditions列表转换为WhereConditions列表:

想象一下。你有一张长颈鹿的名单,方法是要一张动物的名单

在不知道它们将如何处理列表中的动物(如尝试添加马)的情况下,这些类型是不兼容的,但如果您将其更改为以下内容:

        public static string MultiWhereToString(IEnumerable<WhereCondition> whereConditions)
        {
            return null;
        }
公共静态字符串MultiWhereToString(IEnumerable whereConditions)
{
返回null;
}
然后方差可以开始,并且