1

我正在用 C# 编写一个随机值生成器,这个通用函数应该返回 , , 的值,具体Type Bool取决于传递的类型。所以,我可以将作为方法参数传递,但是我应该如何定义返回类型呢?Int64Int32DoubleSystem.Type

例如

GetRandomValueByType(TypeCode.Boolean) <--- 返回布尔值 GetRandomValueByType(TypeCode.Double) <--- 返回双 GetRandomValueByType(TypeCode.Int32) 精度 <--- 返回 Int32

等等等等。

谢谢!

- - - - - - - - - - - - - -编辑 - - - - - - - - - - - ----------

这是我使用的代码:

if (ta.IsPrimitive || Type.GetTypeCode(ta) == TypeCode.String)
{
    Random rnd = new Random();
    var buffer = new byte[sizeof(Int64)];
    rnd.NextBytes(buffer);
    switch (Type.GetTypeCode(ta))
    {
        case TypeCode.Boolean:
            oArr[ctr] = (rnd.Next(100) % 2 == 0);
            break;
        case TypeCode.Byte:
            oArr[ctr] = buffer[0];
            break;
        case TypeCode.SByte:
            oArr[ctr] = (sbyte)buffer[0];
            break;
        case TypeCode.Char:
            oArr[ctr] = Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65));
            break;
        case TypeCode.Decimal:
            oArr[ctr] = NextDecimal(rnd);
            break;
        case TypeCode.Double:
            oArr[ctr] = rnd.NextDouble() * rnd.Next(Int32.MaxValue);
            break;
        case TypeCode.Single:
            var buf = new byte[sizeof(Single)];
            rnd.NextBytes(buf);
            oArr[ctr] = BitConverter.ToSingle(buffer, 0);
            break;
        case TypeCode.Int32:
            oArr[ctr] = rnd.Next(Int32.MinValue, Int32.MaxValue);
            break;
        case TypeCode.UInt32:
            oArr[ctr] = rnd.Next(Int32.MaxValue) + (rnd.Next(100) % 2) * rnd.Next(Int32.MaxValue);
            break;
        case TypeCode.Int64:
            oArr[ctr] = BitConverter.ToInt64(buffer, 0);
            break;
        case TypeCode.UInt64:
            oArr[ctr] = BitConverter.ToUInt64(buffer, 0);
            break;
        case TypeCode.Int16:
            oArr[ctr] = rnd.Next(Int16.MaxValue);
            break;
        case TypeCode.UInt16:
            oArr[ctr] = rnd.Next(Int16.MaxValue) + (rnd.Next(100) % 2) * rnd.Next(Int16.MaxValue);
            break;
        case TypeCode.String:
            oArr[ctr] = RandomString(rnd.Next(100));
            break;
        default:
            oArr[ctr] = 0;
            break;
    }
}
else
{
    oArr[ctr] = getInstance(dllFile, ta.Name);
}
4

3 回答 3

4

泛型在这里可能很方便-您可以执行以下操作:

T GetRandomValueByType<T>() where T : IConvertible
{
   // Compute random...
   return Convert.ChangeType(randomValue, typeof(T));
}

这将通过以下方式调用:

double value = GetRandomValueByType<double>();

但是,这并不完全安全(使用也不安全System.Type),因为您仍然可以传递一个已实现IConvertible但不合适的类型。


鉴于您的评论,我建议您只使用单独的方法:

bool GetRandomBoolean() { // ...
double GetRandomDouble() { // ...
int GetRandomInt() { // ...

如果您已经要打开类型并进行特定的实现,这将提供一种更清洁、更安全的方式来处理它。通过使用单独的方法,您消除了传递错误类型的可能性,并简化了 API。

于 2012-12-12T22:08:28.187 回答
1

您需要使这些方法通用:

T GetRandomValueByType<T>()
{
    ...
}

GetRandomValueByType<Boolean>();
GetRandomValueByType<Double>();
GetRandomValueByType<Int32>();
于 2012-12-12T22:08:27.500 回答
0

你是一个通用的实现:

    public class Generic
    {
      public static T GetRandomValueByType<T>(T parameter)
      {
        switch(typeof(T))
        {
            case System.Int32:
                Random r = new Random();
                return (T)r.Next();
            case System.Guid:
                return (T)Guid.NewGuid();
            // other cases here...
}
      }
    }

并使用这样的东西:

var value = Generic.GetRandomValueByType<bool>();

您还可以使用Random类提供一些随机值,作为样本。

于 2012-12-12T22:10:34.607 回答