C#是否有更好的方法在泛型类型上使用Switch-Case/If-Else [英] C# Is there any better way to use Switch-Case / If-Else on generic types

查看:143
本文介绍了C#是否有更好的方法在泛型类型上使用Switch-Case/If-Else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试对泛型类型进行一些转换.因此,基本思想是拥有一个接受通用Type的方法,并根据传入的类型做不同的事情.

I am currently trying to get some casting on generic types done. So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in.

出于简单的原因,我将展示 float bool default

For simplicity reasons I will just showcase the use of float, bool and default

设置看起来像这样(其中 T 是由类本身定义的通用类型):

The setup looks something like this (where T is a generic type defined by the class itself):

protected T DoStuff(T value)
{
   switch (value) {
      case float floatValue:
         float result = DoFloatStuff(floatValue);
         switch (result) {
            case T output:
               return output;
         }
      case bool boolValue:
         bool result = DoBoolStuff(boolValue);
         switch (result) {
            case T output:
               return output;
         }
      default:
         // return value;
         DoRealGenericStuff(value) // Edited, since I just want to sort out some special cases
   }
}

其中 DoFloatStuff DoBoolStuff 只是具有1个参数和其类型的返回类型的方法.

Where DoFloatStuff and DoBoolStuff are just methods that have 1 parameter and a return type of their types respectively.

如果我不那样做(我之前尝试过一些 typeof()强制转换),编译器总是会抱怨无法将其从 T 强制转换为 float ,反之亦然,即使我确保某些Case-Switch/If-Else语句也是如此.

If I don't do it like that (I tried some typeof() casting before), the compiler always complains that it cannot cast from T to float and vice versa, even tho I made sure that would be the case with some Case-Switch / If-Else statements.

有人知道更好的方法吗?

Does anybody know some better way to do this?

预先感谢,BOTHLine

Thanks in advance, BOTHLine

由于很多人都说过同样的话,所以我要么根本不应该使用泛型,要么我的方法本身就需要更多的泛型.我的问题是我需要使用第三方方法来处理我要检查的特殊情况(在这种情况下为 float bool 类型).对于默认情况,我已经以通用方式处理了所有事情.但是对于某些已定义的类型,我只是无法做到这一点.

Since a lot of people said kind of the same thing, that I either shouldn't use generics at all in a case like that or my methods would need to be more generic themselves.. My problem here is that I need to use 3rd party methods to handle the special cases I'm checking for (in this case the float and bool types). For the default case I already handled everything in a generic way. But for some defined types I just can't do that.

所以要详细一点,为什么会这样:我目前正在为"Unity Engine"开发插件.Unity内置了显示类型的方法(所有原始类型和某些特定于Unity的类型).我有一个通用的Wrapper类,应该可以包含任何类型.但是,当我想在Unity Editor提供的GUI中显示该包装的内容时,我必须使用内置方法来显示基元(诸如 UnityEditor.EditrGUI.FloatField()之类的东西)).没有这些方法,我将永远无法显示任何内容.然后,可以按更通用的方式显示可以细分为这些类型的任何其他内容.

So to go a little more in detail why that's the case: I'm currently working on a Plugin for the "Unity Engine". Unity has built in methods to display types (kind of all primitive types and some Unity-specific types). I have a generic Wrapper class which should be able to contain any types. But then when I want to display the content of that wrapper in the GUI that Unity Editor offers, I have to use the built-in methods to display the primitives (something like UnityEditor.EditrGUI.FloatField()). Without those methods I am never able to display anything. Anything else which can be broken down to those types can then be displayed in a more generic way.

推荐答案

所以基本思想是要有一个方法,该方法可以接受通用类型,并根据传入的类型做不同的事情.

So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in.

那是错误的想法.这不是使用泛型的方式.在这种情况下,应该使用方法重载为每种类型分别创建 DoStuff :

That is the wrong idea. It's not the way generics should be used. In such a case, you should use method overloading to create the DoStuff for each type individually:

float DoStuff(float value) {/* float implementation here */}
bool DoStuff(bool value) {/* bool implementation here */}
...

泛型的要点是使您能够编写将在不同类型上运行的代码-因此应在类型本身与代码无关的地方使用它(例如在任何泛型集合中)或可以执行使用接口或基类,所有可接受的类型都将实现(或继承).此时,通常仅在需要一种方法返回接口(或基类)的特定实现时才需要泛型.

The point of generic is to enable you to write a code that will run the same on different types - so it should either be used where the type itself is irrelevant to the code (like in any generic collection) or can be executed with an interface or a base class all the acceptable types implement (or inherits). At this point, generics is usually only needed when you want a method to return a specific implementation of an interface (or base class).

这篇关于C#是否有更好的方法在泛型类型上使用Switch-Case/If-Else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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