[英]C# override object.ToString() in runtime using reflection
I would like to override object.ToString() for struct in runtime using reflection? 我想在运行时使用反射为结构覆盖object.ToString()吗? I need to change one struct, add new or modify existing override, in some .dll for which I don't have code. 我需要在一个我没有代码的.dll中更改一个结构,添加新的或修改现有的覆盖。 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? 如果答案为否,那么:课堂上答案是否不同?
You cannot change the implementation of an existing class or struct using reflection. 您不能使用反射更改现有类或结构的实现。 Sorry. 抱歉。
Wouldn't it be easier to just use an extension method ToStringNew()
? 仅使用扩展方法ToStringNew()
会更容易吗?
public static class ExtensionMethods
{
public static string ToStringNew(MyStruct str)
{
return ...;
}
}
I thought that's what extension methods were designed for... 我认为那是设计扩展方法的目的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.