猴子修补在C# [英] Monkey Patching in C#

查看:193
本文介绍了猴子修补在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时扩展或修改C#类的代码?

Is it possible to extend or modify the code of a C# class at runtime?

我的问题专门围绕着Monkey Patching / Duck Punching或Meta Object Programming(MOP ),因为它在脚本语言(如Groovy,Ruby等)中发生。

My question specifically revolves around Monkey Patching / Duck Punching or Meta Object Programming (MOP), as it happens in scripting languages such as Groovy, Ruby etc.

推荐答案


是否可能在运行时扩展或修改C#类的代码?

Is it possible to extend or modify the code of a C# class at run-time?

不可以在.NET中执行此操作。您可以编写派生类和覆盖方法(如果它们是虚拟的),但是不能修改现有类。想象一下,如果您所要求的是可能的:您可以修改一些现有系统类的行为,如System.String。

No it is not possible to do this in .NET. You could write derived classes and override methods (if they are virtual) but you cannot modify an existing class. Just imagine if what you were asking was possible: you could modify the behavior of some existing system classes like System.String.

您还可以查看扩展方法将功能添加到现有类。

You may also take a look at Extension methods to add functionality to an existing class.

这篇关于猴子修补在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆