简体   繁体   中英

How can I make a C# class with a constructor and have it return a string?

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.

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.

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;
        }
    }

Remarks:

  • 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.

Cons of using implicit operator:

  • 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.

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
                                             );

Implicit operators :

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM