简体   繁体   English

是否可以在运行时将方法添加到现有类? 为什么或者为什么不?

[英]Is it possible to add a method to an EXISTING class at runtime? why or why not?

I would imagine this might use Reflection.Emit, 我想这可能会使用Reflection.Emit,

but a similar question on SO only answers how to create a class/method dynamically, not how to update an existing class. 但是关于SO的类似问题只能回答如何动态创建类/方法,而不是如何更新现有类。

In a similar vein, is it possible to delete methods / classes at runtime? 同样,是否可以在运行时删除方法/类? If so, I suppose one could just delete the class, and add it back with its old methods plus the new one. 如果是这样,我想一个人可以删除该类,然后用其旧方法和新方法将其添加回去。

Thanks in advance. 提前致谢。

PS I don't have an intended use for this, it is merely a matter of curiosity. PS:我对此没有预期的用途,这只是出于好奇。

In regular C# / .NET, the answer is a simple "no". 在常规C#/ .NET中,答案是简单的“否”。 The most you can do is write a DynamicMethod which can behave like a method of that type (access to private fields etc), but it won't ever be on the API - you just end up with a delegate. 您最多可以做的是编写一个DynamicMethod ,它的行为类似于该类型的方法(访问私有字段等),但是它永远不会出现在API上-您只能得到一个委托。

If you use dynamic , you can do pretty much anything you want. 如果您使用dynamic ,那么您几乎可以做任何您想做的事情。 You can emulate that with ExpandoObject by attaching delegates in place of methods, but on a custom dynamic type you can do most anything - but this only impacts callers who use the dynamic API. 您可以使用ExpandoObject通过代替方法附加委托来模拟这一点,但是在自定义动态类型上,您可以执行任何操作-但这仅影响使用dynamic API的调用者。 For a basic ExpandoObject example: 对于一个基本的ExpandoObject示例:

dynamic obj = new ExpandoObject();
Func<int, int> func = x => 2*x;
obj.Foo = func;
int i = obj.Foo(123); // now you see it
obj.Foo = null; // now you don't

For properties and events (not methods), you can use the System.ComponentModel approach to change what appears at runtime, but that only impacts callers who get access via System.ComponentModel , which means mainly: UI data-binding. 对于属性和事件 (不是方法),可以使用System.ComponentModel方法更改运行时出现的内容,但这仅影响通过System.ComponentModel获得访问权限的调用者,这主要是指:UI数据绑定。 This is how DataTable represents columns as pseudo-properties (forgetting about "typed datasets" for the moment - it works without those). 这就是DataTable将列表示为伪属性的方式(此刻暂时忘记了“类型化数据集”-它无需这些属性即可工作)。

For completeness, I should also mention extension methods . 为了完整起见,我还应该提到扩展方法 Those are more a compiler trick, not a runtime trick - but kinda allow you to add methods to an existing type - for small values of "add". 这些更多的是编译器技巧,而不是运行时技巧,但是有点允许您将方法添加到现有类型中,以获取较小的“ add”值。

One final trick, commonly used by ORMs etc - is to dynamically subclass the type, and provide additional functionality in the subclass. 一个最终招,通过奥姆斯等常用-是动态子类的类型,以及在子类中提供附加的功能。 For example, overriding properties to intercept them (lazy-loading, etc). 例如,覆盖属性以拦截它们(延迟加载等)。

is it possible to delete methods / classes at runtime? 是否可以在运行时删除方法/类?

Suppose that it was possible. 假设有可能。 Calls to those methods would fail and produce undefined (but usually catastrophic) behaviour. 对这些方法的调用将失败,并产生不确定的(但通常是灾难性的)行为。

So I'm sure it is not possible. 因此,我确定这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM