Created
April 17, 2011 18:22
-
-
Save NeilRobbins/924313 to your computer and use it in GitHub Desktop.
How many parameters can a C# method take?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CodeDom; | |
using System.CodeDom.Compiler; | |
using Microsoft.CSharp; | |
namespace LangTests | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int errorCount = 0; | |
decimal numberOfParameters = 1.0m; | |
CompilerResults results = null; | |
while (errorCount < 1) | |
{ | |
var codeDomProvider = new CSharpCodeProvider(); | |
var codeGenerationOptions = new CodeGeneratorOptions(); | |
codeGenerationOptions.BlankLinesBetweenMembers = false; | |
var compileUnit = new CodeCompileUnit(); | |
var generatedNamespace = new CodeNamespace("Generated.Database.Classes"); | |
compileUnit.Namespaces.Add(generatedNamespace); | |
var codeTypeDeclaration = new CodeTypeDeclaration("Foo"); | |
var codeMethod = new CodeMemberMethod(); | |
codeMethod.Name = "Bar"; | |
for (int ordinal = 0; ordinal < numberOfParameters; ordinal++) | |
codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof (object), "baz" + ordinal)); | |
codeTypeDeclaration.Members.Add(codeMethod); | |
generatedNamespace.Types.Add(codeTypeDeclaration); | |
results = codeDomProvider.CompileAssemblyFromDom(new CompilerParameters(), compileUnit); | |
errorCount = results.Errors.Count; | |
if (numberOfParameters%100.0m == 1.0m) Console.WriteLine(numberOfParameters + " Parameters"); | |
numberOfParameters++; | |
} | |
Console.WriteLine("Completed with" + numberOfParameters + " parameters"); | |
Console.WriteLine("Errors were:"); | |
foreach (CompilerError error in results.Errors) | |
{ | |
Console.WriteLine(error.ErrorText); | |
} | |
Console.ReadKey(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment