title: Returning value of the Type passed in the function
body: <p>I am writing a random-value generator in C#, this generic function should return value of <code>Type</code> <code>Bool</code>, <code>Int64</code>, <code>Int32</code>, <code>Double</code> depending on the Type passed. So, I can pass the <code>System.Type</code> as a method parameter, but how should I define the return type?</p>
  
  <p>For example</p>
  
  <p><code>GetRandomValueByType(TypeCode.Boolean)</code>  &lt;--- Returns Boolean
  <code>GetRandomValueByType(TypeCode.Double)</code>   &lt;--- Returns Double
  <code>GetRandomValueByType(TypeCode.Int32)</code>  &lt;--- Returns Int32</p>
  
  <p>And so on and so forth.</p>
  
  <p>Thank you! </p>
  
  <p>---------------------------EDIT--------------------------------</p>
  
  <p>This is the code I used:</p>
  
  <pre><code>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);
  }
  </code></pre>