隐式铸造一个通用的< T>回到T [英] implicitly casting a generic<T> back to T

查看:150
本文介绍了隐式铸造一个通用的< T>回到T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写这样的类泛型类 MyGeneric< T> 是有可能写出隐式转换为T类型,所以我可以做的东西,如:

If I write a generic class like class MyGeneric<T> is it possible to write an implicit cast to type T, so I can do stuff like:

public class MyGeneric<T>
{
...
}

public class GenericProperties
{
   public MyGeneric<string> MyGenericString {get;set;}

   public void UseMyGeneric()
   {
       string sTest = MyGenericString;
       MyGenericString = "this is a test";
   }
}

是否有可能做到这一点的超载运营商?我知道它可以做,如果我的课是不是一个普通的...

Is it possible to do that by overloading operators? I know it could be done if my class wasn't a generic...

推荐答案

恩,是的,但对于爱情僵尸耶稣不这样做。这真的很令人困惑。你稍微误解仿制药的目的,我想。它不是用来转一类到该类型,它的使用具有该类型(MyGenericString)是你想要的类型的'知道',出于各种目的(通常这些都是基于集合的目的)。

Well, yes, but for the love of zombie jesus do NOT do that. It's really confusing. You're slightly misunderstanding the purpose of generics, I think. It's not used to "turn" a class into that type, it's used to have that type (MyGenericString) be 'aware' of the type you want, for various purposes (typically those are collection-based purposes).

这篇关于隐式铸造一个通用的&LT; T&GT;回到T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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