[...]
I am not sure I understand what you mean.
I mean your code to create a new instance of Action<TextWriter> makes no
sense. You can't pass a reference to an actual TextWriter instance to a
constructor that expect a method name.
Basically, I have the following:
public static HelperResult Join(this HtmlHelper helper,
HelperResult result1, HelperResult result2) {
return new HelperResult(writer => writer.Write(result1 +
result2.ToString()));
} // HelperResult
That seems pretty goofy to me. Granted, you didn't bother to post the
declarations for HtmlHelper and HelperResult, so maybe there's some
super-awesome functionality they support and which require the above
kind of code.
But on the face of it, it's not at all clear why HelperResult requires a
delegate that will write a specific string to a TextWriter instance,
when you presumably could just store the concatenated string itself and
write it to any arbitrary TextWriter passed to it at any time.
Which I would like to turn into:
public static HelperResult Join(this HtmlHelper helper,
IList<HelperResult> results) {
TextWriter writer = new StreamWriter();
Action action = new Action<TextWriter>(writer);
This can't compile. Not only does it have the same "variable used as a
method" problem you started with said:
foreach (HelperResult result in results)
writer.Write(result);
Your original "Join" method doesn't actually write to the TextWriter.
Why should the new version do so? Furthermore, what is the point of
creating the "action" delegate instance when you never actually use it?
return new HelperResult(new Action<TextWriter>(writer));
HelperResult has only one constructor of type:
HelperResult(Action<TextWriter>(writer));
Why does it have that one constructor? What is the purpose of passing a
TextWriter instance to a HelperResult constructor?
Well, I am not sure I need to do this. My problem would be solved if I
could use the IList<HelperResult> into:
return new HelperResult(writer => writer.Write(result1 +
result2.ToString()));
I think there is a way to, maybe using linq, to do:
result1inlist + result2inlist + result3inlist into:
writer => writer.Write(...Here ...)
But my trials do not work out ....
From all that, it seems all you really want to do is extend your
original "Join" method so that it can join more than just two
HelperResults. Why not just write it like this:
public static HelperResult Join(this HtmlHelper helper, params
HelperResult[] results)
{
StringBuilder sb = new StringBuilder();
foreach (HelperResult result in results)
{
sb.Append(result.ToString());
}
return new HelperResult(writer => writer.Write(sb.ToString()));
}
The TextWriter part still looks to me to have dubious value. But at
least the above ought to be what you seem to be trying to do.
(Note that I choose to support the variable parameter list syntax,
rather than your IList<HelperResult> approach. There's no indication in
your post that you really need an IList<T>; at most, an IEnumerable<T>
should be sufficient. And frankly, for the usage that it appears you're
trying to do, the "params" syntax is likely to be more convenient. If
you start with an IList<T>, just use the ToArray() extension method, and
of course the caller can just list as many HelperResults in the method
call as they like, and the compiler will take care of converting to an
array for you).
Pete