|
| Change ToString() ouput based on calling context? |
 |
Thu, 27 Mar 2008 23:26:39 +000 |
For example.
public class Address { }
In a Console Application new Address().ToString() => "1 Main
St.\r\nAnytown, USA 12345"
In an ASP.net web page <%# new Address() %> => "<address>1
Main St.<br/>Anytown, USA 12345</address>"
If I could do "HTML 4.01" then <%# new Address() %> =>
"<address>1 Main St.<br>Anytown,
USA 12345</address"
|
| Post Reply
|
| Re: Change ToString() ouput based on calling context? |
 |
Fri, 28 Mar 2008 00:15:31 +000 |
"djseng" wrote in message news:2260301@forums.asp.net...
For example.
public class Address { }
In a Console Application new Address().ToString() => "1 Main
St.\r\nAnytown, USA 12345"
In an ASP.net web page <%# new Address() %> => "<address>1
Main St.<br/>Anytown, USA 12345</address>"
If I could do "HTML 4.01" then <%# new Address() %> =>
"<address>1 Main St.<br>Anytown,
USA 12345</address"
http://forums.asp.net/p/1239587/2260301.aspx#2260301
Off the top of my head...you could check the EntryAssembly, or walk loaded
assemblies, looking for some clues as to what environment you're in (I've done
similar to catch differences between IIS and Cassini, for instance). Or, if
you're really only concerned with ASP.NET vs Console, you could check
HttpContext.Current. --MB
|
| Post Reply
|
| Re: Change ToString() ouput based on calling context? |
 |
Fri, 28 Mar 2008 00:34:33 +000 |
I am unfamiliar with this EntryAssembly... A colleague of mine suggested
something like...
public string ToString(string format, IFormatProvider formatProvider)
{
if (typeof(Control).IsAssignableFrom(new
StackFrame(2).GetMethod().DeclaringType))
return"<address>" + this.ToXhtml() +
"</address>";
else
return this.ToString();
}
That's not too smelly is it?
|
| Post Reply
|
| Re: Change ToString() ouput based on calling context? |
 |
Fri, 28 Mar 2008 12:55:51 +000 |
"djseng" wrote in message news:2260465@forums.asp.net...
I am unfamiliar with this EntryAssembly... A colleague of mine suggested
something like...
public string ToString(string format, IFormatProvider formatProvider)
{
if (typeof(Control).IsAssignableFrom(new
StackFrame(2).GetMethod().DeclaringType))
return"<address>" + this.ToXhtml() +
"</address>";
else
return this.ToString();
}
That's not too smelly is it?
http://forums.asp.net/p/1239587/2260465.aspx#2260465
System.Assembly.GetEntryAssembly() should return the managed EXE that started
the whole shebang. In a WPF/WinForms/client app, this should be your EXE. I
think it's null in ASP.NET, because of the way the process is started (unmanaged
ISAPI filter). The StackFrame method may work, but does have some problems.
First, I think it requires unmanaged code permissions - which would prevent it
from running in a shared hosting environment, I think. Second, I don't think
it's a guarantee that (a) the frame you're looking for would always be 2 levels
above you (I assume level 1 would be ToString(), but level 2 could be a helper
method or something), and (b) you would only change output if it was a Control -
which would probably mean calling ToString in the debugger would be different
than what's produced at runtime...that could be a nightmare to debug. As for
the smelliness...well, I'm not sure how you could do this without introducing
some sort of smell. Which may indicate the design itself is a problem - you're
really putting display logic into your model classes. I can see the convenience
win, but I'm not sure it's worth it. A ..ToHtmlString() or a format string would
do the same, and be more explicit. On second thought, creating an AddressControl
would probably be the best approach. --MB
|
| Post Reply
|
| Re: Change ToString() ouput based on calling context? |
 |
Fri, 28 Mar 2008 14:47:25 +000 |
These conclusions are almost identical to the ones I was coming to as well... If
I am able to whip up something rather quickly maybe I'll post it.
Thanks MB!
|
| Post Reply
|
|
|
|
|
|
|
|
|
|