Crazy Linq: render a method invocation and its arguments as a string

Note: this entry has moved.

Context: invocation represents a method invocation, has a method and the array of arguments passed-in. We want to render the call such as: MyClass.MyMethod(1, true, null, "foo"). Note how null values should render as the string "null", and string values should be enclosed in quotes.

Here you go:

return
	invocation.Method.DeclaringType.Name + "." +
	invocation.Method.Name + "(" +
	String.Join(", ",
		(from x in invocation.Arguments
		 select x == null ?
			"null" :
			x is string ?
				"\"" + (string)x + "\"" :
				x.ToString())
		.ToArray()
	) + ")";

No Comments