繁体   English   中英

在函数中传递的Type的返回值

[英]Returning value of the Type passed in the function

我在C#中编写一个随机值生成器,此泛型函数应根据传递的类型返回Type BoolInt64Int32Double值。 因此,我可以将System.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);
}

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

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。

您需要使这些方法通用:

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

GetRandomValueByType<Boolean>();
GetRandomValueByType<Double>();
GetRandomValueByType<Int32>();

您是一个通用的实现:

    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类作为示例提供一些随机值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM