Reputation:
I created the following:
public class HttpException
{
public string Text { get; set; }
public string Message { get; set; }
public string InnerExceptionMessage { get; set; }
public string InnerExceptionInnerExceptionMessage { get; set; }
}
I'm calling the class like this:
var e = new HttpException() {
Text = "City not created",
Message = ex.Message,
InnerExceptionMessage = ex.InnerException.Message,
InnerExceptionInnerExceptionMessage = ex.InnerException.InnerException.Message
};
var jsonMsg = JSON.ToJSONString(e);
Is there a way I could make it so I can call the class with just the parameters of a text message and the exception and then have it return a string jsonMsg
Note that the JSON.ToJSONString(e) is an external class that I am using to form a JSON string.
Upvotes: 2
Views: 123
Reputation: 2385
You can use the implicit operator to make implicit cast of the class like this:
public static void Main()
{
HttpException ex = new HttpException ();
string text = ex;
Console.WriteLine(text);
Console.ReadLine();
}
public class HttpException
{
public string Text = "goofy";
public static implicit operator string(HttpException ex)
{
return ex.Text;
}
}
The implicit operator doesn't stop to string you can make any cast you want.
You can use the explicit operator for doing explicit cast string test = (string)goofy;
which might be somewhat more readable.
It's hardly discoverable (It's hard from someone different from yourself to find this "feature" and yourself can forgot about that if you take the code from a month from now.)
It's make the code more complex to read (someone might thing what the hell is going on here).
It's error prone.
Upvotes: 1
Reputation: 64943
public static implicit operator string(HttpException exception)
{
return JSON.ToJSONString(e);
}
This implicitly converts an HttpException
into a string behind the scenes, so now you can do this:
string json = new HttpException();
Upvotes: 0
Reputation: 82504
As mentioned by zzzzBov, you can simply override the ToString() method. However, if the only use for this object is to create the json string, I would consider creating the class like this:
public class HttpException
{
public string Text { get; set; }
public string Message { get; set; }
public string InnerExceptionMessage { get; set; }
public string InnerExceptionInnerExceptionMessage { get; set; }
public static string CreateJsonString(string Text, string Message,
string InnerExceptionMessage,
string InnerExceptionInnerExceptionMessage) {
return JSON.ToJSONString(new HttpException() {
Text = "City not created",
Message = ex.Message,
InnerExceptionMessage = ex.InnerException.Message,
InnerExceptionInnerExceptionMessage =
ex.InnerException.InnerException.Message});
}
}
then all you have to write in your code is:
var jsonMsg = HttpException.CreateJsonString("City not created",
ex.Message,
ex.InnerException.Message,
ex.InnerException.InnerException.Message
);
Upvotes: 0
Reputation: 39277
You mean you want a constructor like this?
public class HttpException
{
public string Text { get; private set; }
private readonly Exception ex;
public string Message { get { return this.ex.message; } }
public string InnerExceptionMessage { get { return this.ex.... } }
public string InnerExceptionInnerExceptionMessage { get { return this.ex....} }
public HttpException(string text, Exception ex)
{
this.Text = text;
this.Exception = ex;
}
}
I still wouldn't put the JSON code in there though, that's a separate concern and you don't normally want to mix serialization code with regular class code.
Upvotes: 3