C#泛型:投泛型类型为值类型 [英] C# generics: cast generic type to value type

查看:546
本文介绍了C#泛型:投泛型类型为值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用类指定类型T.
的值保存值可以是int,UINT,双或浮动。
现在我想要得到的值的字节其编码为一个特定的协议。
所以我想使用的方法BitConverter.GetBytes()但遗憾的是Bitconverter不支持泛型类型或不确定的对象。这就是为什么我要投的价值和调用GetBytes会的)具体的过载(。
我的问题:
我如何可以投一个通用的价值为int,double或float?
这不起作用:

I have a generic class which saves value for the specified type T. The value can be an int, uint, double or float. Now I want to get the bytes of the value to encode it into an specific protocol. Therefore I want to use the method BitConverter.GetBytes() but unfortunately Bitconverter does not support generic types or undefined objects. That is why I want to cast the value and call the specific overload of GetBytes(). My Question: How can I cast a generic value to int, double or float? This doesn't work:

public class GenericClass<T>
    where T : struct
{
    T _value;

    public void SetValue(T value)
    {
        this._value = value;
    }

    public byte[] GetBytes()
    {
        //int x = (int)this._value;
        if(typeof(T) == typeof(int))
        {
            return BitConverter.GetBytes((int)this._value);
        }
        else if (typeof(T) == typeof(double))
        {
            return BitConverter.GetBytes((double)this._value);
        }
        else if (typeof(T) == typeof(float))
        {
            return BitConverter.GetBytes((float)this._value);
        }
    }
}



有投可能性一个通用的价值?
或有另一种方式来获得字节?

Is there a possibility to cast an generic value? Or is there another way to get the bytes?

推荐答案

首先,这是一个非常糟糕的代码味道。任何你正在做的这样的赔率类型参数型测试时都好你滥用仿制药。

First off, this is a really bad code smell. Any time you're doing a type test on a type parameter like this odds are good you're abusing generics.

C#编译器知道你在这个仿制药滥用方式禁止了在T类型的值强制转换为int等,您可以通过铸造价值,你将它转换为int之前反对关闭编译器在你的方式:

The C# compiler knows that you are abusing generics in this way and disallows the cast from the value of type T to int, etc. You can turn off the compiler getting in your way by casting the value to object before you cast it to int:

return BitConverter.GetBytes((int)(object)this._value);



呸。再次,这将更好地找到另一种方式来做到这一点。例如:

Yuck. Again, it would be better to find another way to do this. For example:

public class NumericValue
{
    double value;
    enum SerializationType { Int, UInt, Double, Float };
    SerializationType serializationType;        

    public void SetValue(int value)
    {
        this.value = value;
        this.serializationType = SerializationType.Int
    }
    ... etc ...

    public byte[] GetBytes()
    {
        switch(this.serializationType)
        {
            case SerializationType.Int:
                return BitConverter.GetBytes((int)this.value);
            ... etc ...

没有泛型必要的。储备仿制药是实际情况的通用的。如果你写的代码的四次的一个每种类型的,你有没有泛型获得任何东西。

No generics necessary. Reserve generics for situations that are actually generic. If you've written the code four times one for each kind of type, you haven't gained anything with generics.

这篇关于C#泛型:投泛型类型为值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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