C# 在运行时使用反射重写 object.ToString()

发布于 2024-12-26 05:48:11 字数 140 浏览 5 评论 0原文

我想在运行时使用反射覆盖结构的 object.ToString() ? 我需要在一些我没有代码的 .dll 中更改一个结构、添加新结构或修改现有覆盖。所以我想在运行时完成它。

是否可以?这个教程会很棒吗? 如果答案是否定的,那么:班级的答案是否不同?

I would like to override object.ToString() for struct in runtime using reflection?
I need to change one struct, add new or modify existing override, in some .dll for which I don't have code. So I am thinking to do it in runtime.

Is it possible? Tutorial for this would be excellent?
If answer is NO then: is answer different for class?

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

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

发布评论

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

评论(2

慈悲佛祖 2025-01-02 05:48:11

您无法使用反射更改现有类或结构的实现。对不起。

You cannot change the implementation of an existing class or struct using reflection. Sorry.

内心旳酸楚 2025-01-02 05:48:11

仅使用扩展方法 ToStringNew() 不是更容易吗?

public static class ExtensionMethods
{
    public static string ToStringNew(MyStruct str)
    {
        return ...;
    }
}

我认为这就是扩展方法的设计目的......

Wouldn't it be easier to just use an extension method ToStringNew()?

public static class ExtensionMethods
{
    public static string ToStringNew(MyStruct str)
    {
        return ...;
    }
}

I thought that's what extension methods were designed for...

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