如何允许我的类隐式转换为 C# 中的字符串?

发布于 2024-09-08 21:16:04 字数 293 浏览 9 评论 0原文

这就是我想要做的...

public class A
{
    public string Content { get; set; }
}

A a = new A();
a.Content = "Hello world!";
string b = a; // b now equals "<span>Hello world!</span>"

所以我想控制 如何 a 转换为 String...不知何故...

Here's what I want to do...

public class A
{
    public string Content { get; set; }
}

A a = new A();
a.Content = "Hello world!";
string b = a; // b now equals "<span>Hello world!</span>"

So I want to control how a is converted into a String… somehow…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枉心 2024-09-15 21:16:04

您可以手动覆盖类的隐式和显式强制转换运算符。 教程在这里。不过,我认为大多数时候它的设计很糟糕。我想说,如果你写的话会更容易看到发生了什么

string b = a.ToHtml();

但这当然是可能的...

public class A
{
    public string Content { get; set; }

    public static implicit operator string(A obj)
    {
        return string.Concat("<span>", obj.Content, "</span>");
    }
}

为了给出一个为什么我不推荐这样做的例子,请考虑以下内容:

var myHtml = "<h1>" + myA + "</h1>";

上面的将产生 "

; Hello World!

"

现在,其他一些开发人员出现并认为上面的代码看起来很糟糕,并将其重新格式化为以下内容:

var myHtml = string.Format("<h1>{0}</h1>", myA);

但是 string.Format 在内部为它接收到的每个参数调用 ToString,因此我们不再处理隐式转换,因此,其他开发人员将把结果更改为类似于

myNamespace.A

You can manually override the implicit and explicit cast operators for a class. Tutorial here. I'd argue it's poor design most of the time, though. I'd say it's easier to see what's going on if you wrote

string b = a.ToHtml();

But it's certainly possible...

public class A
{
    public string Content { get; set; }

    public static implicit operator string(A obj)
    {
        return string.Concat("<span>", obj.Content, "</span>");
    }
}

To give an example of why I do not recommend this, consider the following:

var myHtml = "<h1>" + myA + "</h1>";

The above will, yield "<h1><span>Hello World!</span></h1>"

Now, some other developer comes along and thinks that the code above looks poor, and re-formats it into the following:

var myHtml = string.Format("<h1>{0}</h1>", myA);

But string.Format internally calls ToString for every argument it receives, so we're no longer dealing with an implicit cast, and as a result, the other developer will have changed the outcome to something like "<h1>myNamespace.A</h1>"

伊面 2024-09-15 21:16:04
public class A
{
    public string Content { get; set; }
    public static implicit operator string(A a)
    {
        return string.Format("<span>{0}</span>", a.Content);
    }
}
public class A
{
    public string Content { get; set; }
    public static implicit operator string(A a)
    {
        return string.Format("<span>{0}</span>", a.Content);
    }
}
奶茶白久 2024-09-15 21:16:04
public static implicit operator string(A a)
{
    return "foo";
}
public static implicit operator string(A a)
{
    return "foo";
}
李不 2024-09-15 21:16:04

重写 ToString() 是个好方法。此外,在调试模式下,您会得到有关 ToString() 返回值的提示。

overriding ToString() would be nice way. Moreover in debug mode you got a tip with ToString() return value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文