如何创建一个带有构造函数的c#类并让它返回一个字符串?

本文关键字:一个 字符串 返回 何创建 创建 构造函数 | 更新日期: 2023-09-27 18:09:38

我创建了以下内容:

public class HttpException
{
    public string Text { get; set; }
    public string Message { get; set; }
    public string InnerExceptionMessage { get; set; }
    public string InnerExceptionInnerExceptionMessage { get; set; }
}

我像这样调用这个类:

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

是否有一种方法,我可以使它,以便我可以调用类与文本消息和异常的参数和然后让它返回字符串jsonMsg

请注意JSON. tojsonstring (e)是一个外部类,我使用它来形成JSON字符串。

如何创建一个带有构造函数的c#类并让它返回一个字符串?

你的意思是你想要这样的构造函数?

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

我仍然不会把JSON代码放在那里,但这是一个单独的问题,你通常不希望将序列化代码与常规类代码混合。

可以使用隐式操作符对类进行隐式强制转换,如下所示:

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

备注:

  • 隐式操作符不会停止到string,你可以任意强制转换

  • 您可以使用显式操作符进行显式强制转换string test = (string)goofy;,这可能会更容易读。

隐式操作符的缺点:

  • 这很难被发现(很难从不同的人身上发现这个"特性",如果你从现在开始一个月的代码,你可能会忘记这一点。)

  • 这会让代码读起来更复杂(有人可能会想这到底是怎么回事)

正如zzzzBov所提到的,您可以简单地重写ToString()方法。然而,如果这个对象的唯一用途是创建json字符串,我会考虑这样创建类:

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

那么你所要写的代码就是:

var jsonMsg = HttpException.CreateJsonString("City not created", 
                                              ex.Message,
                                              ex.InnerException.Message,
                                              ex.InnerException.InnerException.Message
                                             );

隐式操作符:

public static implicit operator string(HttpException exception)
{
    return JSON.ToJSONString(e);
}

这在幕后隐式地将HttpException转换为字符串,所以现在您可以这样做:

string json = new HttpException();