我如何能实现基本类型的延伸性能等级以干净的方式? [英] How can I implement an extention property class for primitive Types in a clean way?

查看:109
本文介绍了我如何能实现基本类型的延伸性能等级以干净的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Basicly我想是产生哪些没有给定值的值相同的其他随机值的。

Basicly what I would like to is generate Other random values which have not the same value of the given value.

我有点不能得到的原始类型的持有在一个不错的方式。

I somehow can not get hold of the primitive Types in a nice way.

的基本类型有布尔,字节,为SByte,Int16的,UINT16,的Int32,UInt32的,Int64的,UINT64,IntPtr的,UIntPtr,字符,双,和单。

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

下面是basicly什么,我试图做的。

Here is basicly what I am trying to do.

  int oldValue = 1;
  oldValue.Other(); // 2

  long oldValue = 1;
  oldValue.Other(); // 2

  string oldValue = "1";
  oldValue.Other(); "5"



有人曾经建议,我怎么能解决这个好听?

Has someone suggestions how I could tackle this nicely?

推荐答案

有一个名为值类型的基类。问题是,你需要将值转换为孩子使用时。

There is a base class called ValueType. The problem is that you need to cast the value to child when using it.

例如

int a =3;
int b = (int)a.abc();



扩展看起来像下面

extension look like following

public static class ValueTypeExtenstion
{
    public static ValueType abc(this ValueType a) {

        return default(ValueType);
    }
}

您必须对参数进行类型检查'一'在如果ELSEIF正确回到你想要的值。

you have to perform type check on parameter 'a' in if elseif to correctly return the value you intend to.

例如

    if( a is Int32 )
      return 4;



更新:
串不完全是值类型,但它当作之一。你仍然必须处理在一个单独的扩展方法的字符串。

Update: string is not exactly a value type but its treated like one. You still have to handle string in a seperate extension method.

这篇关于我如何能实现基本类型的延伸性能等级以干净的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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