title: generate random parameter value for a method without knowing the parameter type
body: <p>What problem do I want to solve? Via reflection I want to execute code (running instance methods and also static methods) where I dont know beforehand how the method to be run is defined.</p>
  
  <p>Lets say I have a <code>MethodInfo</code> that I want to invoke. I have no idea what parameters it has, so I do following.</p>
  
  <p>first I check that method params are valid (if not valid then method is not invoked):</p>
  
  <pre><code> private static IEnumerable&lt;Type&gt; GetValidMethodTypes()
      {
          var validTypes = new List&lt;Type&gt;();
          validTypes.AddRange(new[]
          {
              typeof (SByte),
              typeof(String[]),
              //etc...
          });
  
          return validTypes;
      }
  </code></pre>
  
  <p>Then I generate random values based on param type:</p>
  
  <pre><code> public object RandomizeParamValue(string typeName)
      {
          switch (typeName)
          {
              case "SByte":
              {
                  //return randomized value
              }
              case "String[]":
              {
                  //return randomized value
              }
              //etc...
          }
       }
  </code></pre>
  
  <p>for example, a randomized value for String[] would be ["a","ab", "ccc"] or ["aa", "b"]. The formula is: new string[random size between 1 and 5 ] that contains random strings with random lengths. Everything super random :)</p>
  
  <p>The problem is that this works only with what types I support in my code. I could offcourse add support for many many more types but it is much work. I would like to make this much more generic but I dont really know how. I have googled this but found nothing. Anyone knows of a solution/known pattern for this kind of problem?   </p>
  
  <p>I can re-formulate the question like this: How can I, via reflection, invoke following method:</p>
  
  <pre><code>void SomeMethod(unknowntypeatcompiletime param);
  </code></pre>
  
  <p>where unknowntypeatcompiletime could be anything.</p>