C# 我可以从返回类型';s型?
所以我有,比方说,这种方法:C# 我可以从返回类型';s型?,c#,generics,C#,Generics,所以我有,比方说,这种方法: public ICollection<String> doSomething() { } public ICollection doSomething(){} 目前,我正在尝试检查方法的返回类型是否为ICollection类型。然而,在C#中,我在进行检查时必须通过泛型。所以我不能说,“方法是ICollection” 问题是我不想在检查时限制泛型的类型。在Java中,我可以只使用通配符,但在C#中不能这样做。我曾想过尝试使用Type.getGener
public ICollection<String> doSomething() { }
public ICollection doSomething(){}
目前,我正在尝试检查方法的返回类型是否为ICollection类型。然而,在C#中,我在进行检查时必须通过泛型。所以我不能说,“方法是ICollection”
问题是我不想在检查时限制泛型的类型。在Java中,我可以只使用通配符,但在C#中不能这样做。我曾想过尝试使用Type.getGenericParameterContraints()并尝试将它的第一个结果粘贴到ICollection的泛型约束中进行检查,但这也不起作用。有人有什么想法吗
isCollection(MethodInfo method){
Type[] ret = method.ReturnType.GetGenericParametersContraint();
Type a = ret[0];
return method.ReturnType is ICollection<a>;
}
isCollection(MethodInfo方法){
Type[]ret=method.ReturnType.GetGenericParametersContraint();
a型=ret[0];
return method.ReturnType为ICollection;
}
编辑:添加了我尝试的内容。您应该能够执行以下操作:
MethodInfo method = ... // up to you
var returnType = method.ReturnType;
var isGenericICollection = returnType == typeof(ICollection<>);
MethodInfo方法=…//由你决定
var returnType=方法.returnType;
var isGenericCollection=returnType==typeof(ICollection);
使用,并将其结果与类型(ICollection)
进行比较
因此,要检查方法的返回类型是否为ICollection
,可以这样做:
method.ReturnType.GetGenericTypeDefinition() == typeof(ICollection<>)
method.ReturnType.GetGenericTypeDefinition()==typeof(ICollection)
顺便说一句,method.ReturnType is ICollection
永远不会为真,因为is
检查第一个操作数的类型是否是第二个操作数的子类型ReturnType
属于type
类型,虽然它不是某些ICollection
的子类型
Use the below method, call `isCollection<string>(method)`
public static bool isCollection<T>(MethodInfo method)
{
return method.ReturnType.Equals(typeof(ICollection<T>));
}
使用下面的方法,调用'isCollection(method)`
公共静态bool isCollection(MethodInfo方法)
{
return方法.ReturnType.Equals(typeof(ICollection));
}
如果允许使用非通用的System.Collections.ICollection
(也由ICollection
实现),那么它只是:
typeof(System.Collections.ICollection).IsAssignableFrom(method.ReturnType)
如果您只想与genericICollection
(我看不出有什么理由,但您可能有自己的理由):
method.ReturnType.IsGenericType
&&类型(ICollection)
.IsAssignableFrom(方法.ReturnType.GetGenericTypeDefinition())
请注意,如果返回类型是非泛型的,则这不起作用。因此,如果有一个类实现了ICollection
,但它本身不是泛型的,那么它将不起作用。这意味着它不会捕获类Foo:ICollection
,但会捕获类Foo:ICollection
不过,第一种方法可以很好地解决这两个问题。试试以下方法:
class Program
{
public ICollection<string> Foo() { return new List<string>(); }
public static bool TestType()
{
MethodInfo info = typeof(Program).GetMethod("Foo");
return info.ReturnType.GetGenericTypeDefinition() == typeof(ICollection<>);
}
static void Main(string[] args)
{
Console.WriteLine("{0} is ICollection<> : {1}", "Foo", TestType());
}
}
类程序
{
公共ICollection Foo(){返回新列表();}
公共静态bool TestType()
{
MethodInfo=typeof(Program).GetMethod(“Foo”);
return info.ReturnType.GetGenericTypeDefinition()==typeof(ICollection);
}
静态void Main(字符串[]参数)
{
WriteLine(“{0}是ICollection:{1}”,“Foo”,TestType());
}
}
打印
Foo is ICollection:True
typeof(ICollection)==typeof(ICollection)
isfalse
。无IDE编码。。。启动VMIt与我不为ICollection设置约束时不同。运行时类型从来不是ICollection
,而是具体类型listlist
。所以让类型匹配起来是很棘手的。噢,废话。我刚刚意识到我给这两种方法起的名字是一样的。对此我真的很抱歉。所以,我有一个方法,“doSomething”,它返回一些集合、ICollection(或ArrayList或其他什么)。然后我有另一个方法,isCollection,它检查传递的方法是否是一个集合。自我提醒,多睡觉。但是当我调用isCollection时,我不需要传递一个调用它的约束吗?当我调用它时,我将没有约束。
class Program
{
public ICollection<string> Foo() { return new List<string>(); }
public static bool TestType()
{
MethodInfo info = typeof(Program).GetMethod("Foo");
return info.ReturnType.GetGenericTypeDefinition() == typeof(ICollection<>);
}
static void Main(string[] args)
{
Console.WriteLine("{0} is ICollection<> : {1}", "Foo", TestType());
}
}