通用类型约束禁止某些类型? [英] Generic type constraint to prohibit certain types?

查看:101
本文介绍了通用类型约束禁止某些类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通用方法:


$ b

MvcHtmlString MyMethod< T>(此HtmlHelper html,字符串标题,IEnumerable< T>值)

我的代码中有很多变量,它们是 List< T> IEnumerable< T> , xxxCollection T [] 等等...这个方法也有一个重载,它会取非枚举值。是否可以禁止类型参数约束中的特定类(例如 string )?



我已经创建一个像这样的过载:


$ b

MvcHtmlString MyMethod< T>(this HtmlHelper html,string title,对象值)

这个过载对处理单个值很有好处,但处理值的集合需要实施略有不同。然而, string implements IEnumerable 所以我所有的字符串变量都会被发送到错误的重载,除非我能告诉编译器他们应该被排除在外。

解决方案

您无法使用约束条件,并且您不需要它们,考虑到您打算处理字符串

给定您接受类型 object 的现有方法,类型为 string 因为 IEnumerable< char> 对象更好匹配,所以code> 。但是,您不必放弃重载的想法,也不需要求助于运行时检查。只需为 string 创建一个额外的重载。通过使编译器的场景变得更加简单,完成 object IEnumerable< T> 之间的整个决定。



给定

  void Foo(对象参数)
{
Console.WriteLine(object);


void Foo< T>(IEnumerable< T>变元)
{
Console.WriteLine(enumerable T);


void Foo(字符串参数)
{
Console.WriteLine(string);

方法调用列表

  Foo(hello); 
Foo(1);
Foo(new int [] {1});

生成输出

  string 
object
enumerable T

  void Foo(字符串参数)
{
// Console.WriteLine(string);
Foo((对象)参数);
}


I'd like to create a generic method:

MvcHtmlString MyMethod<T>(this HtmlHelper html, string title, IEnumerable<T> value)

I have many variables in my code which are List<T>, IEnumerable<T>, xxxCollection, T[], etc... This method also has an overload which will take non enumerable values. Is it possible to prohibit specific classes (such as string) in the type parameter constraints?

I've already created one overload like this:

MvcHtmlString MyMethod<T>(this HtmlHelper html, string title, object value)

This overload is good for dealing with individual values, but dealing with collections of values requires a slightly different implementation. However, string implements IEnumerable so all my string variables will be sent to the wrong overload unless I can tell the compiler that they should be excluded.

解决方案

You could not use constraints for this and you do not need them, considering your intention of dealing with string.

Given your existing method that accepts type object, an argument of type string would result in your proposed new overload, since IEnumerable<char> is a better match than object. However, you need not abandon your thought of having an overload, nor should you need to resort to runtime checking. Simply create an additional overload for string. Sidestep the whole decision between object and IEnumerable<T> by making the scenario even easier for the compiler.

Given

void Foo(object argument)
{
    Console.WriteLine("object");
}

void Foo<T>(IEnumerable<T> argument)
{
    Console.WriteLine("enumerable T");
}

void Foo(string argument)
{
    Console.WriteLine("string");
}

An method invocation list of

Foo("hello");
Foo(1);
Foo(new int[] { 1 });

Produces the output

string
object
enumerable T

You could then further coerce your string into your object overload in a single place.

void Foo(string argument)
{
    // Console.WriteLine("string");
    Foo((object)argument);
}

这篇关于通用类型约束禁止某些类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆