如何创建动态方法 [英] how to create dynamic method

查看:65
本文介绍了如何创建动态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在设计专家系统,并且正在使用3层体系结构.我有一个称为业务层和派生类的基类,并使用CodeDom动态创建了这些类,这些类都是从业务层继承的,我想在业务层中动态创建方法,派生类和动态创建的类将进一步调用该方法. >
任何人都可以帮助我
在此先感谢

Hi,
I am designing expert system and i am using 3 layerd architecture. I have base class called business layer and derived classes and dynamiclly created classees using CodeDom these all are inherited from business layer,i want to dynamically create method in a business layer which will be furher called by derived classes and dynamically created classes.

anyone can help me outt
thanks in advance

推荐答案

没有诸如动态添加方法"之类的东西.您可以修改规则条件,以使方法产生不同的结果,但是方法总是会保持编写方式.
There''s no such things as "adding methods dynamically". You can modify rule criteria that can result in a method tgenerating different results, but the methods are ALWAYS going to remain the way they are written.


我不确定您对动态方法.如果要在运行时注入"代码,那么这不是一个好主意...(难以调试,存在很多安全问题,等等...)

但是,如果您想添加脚本功能,则可能会有所帮助:
.NET 2.0的C#脚本 [
I am not sure what you mean with dynamic method. If you want to "inject" code at runtime, then it is not a good idea... (hard to debug, lots of security issues, and so on...)

But if you want to add scripting features, then this could help:
C# Script for .NET 2.0[^]


从上面的评论中,我认为您想要一个系统,您可以在运行时添加规则,您希望可以从已编译的代码中调用这些规则.正确吗?

在继续阅读之前,请确保这是您真正需要的.很少需要在运行时创建新代码,并且显然,这样做会失去JIT编译的所有好处.基于插件的系统(在启动/运行时加载已编译的程序集,如果希望用户能够创建插件,则使用已发布的API)通常是解决该问题的更好方法.例如,请参见
此处或我的游戏大厅服务器和客户端的代码(文章为这里,但并未真正提及插件部分.

如果是这样,这是应在何处使用接口的主要示例.在已编译的代码中定义一个,就像
From your comment above I think you want a system whereby you can add rules at runtime, which you want to be callable from compiled code. Is that correct?

Before you read on, make sure that that''s what you actually need. It is rare to need to create new code at runtime, and obviously you lose all the benefits of JIT compilation if you do so. A plugin based system (loading compiled assemblies at startup/runtime, with a published API if you want the users to be able to create plugins) is usually a better way to solve that problem. For example see here or the code for my game lobby server and client (article is here but doesn''t really mention the plugin part).

If so, this is a prime example of where you should use an interface. Define one in the compiled code, something like
interface IRuleProvider {
  bool IsValid(Claim claim);
}


很明显,该函数的签名应与您希望动态方法执行的操作相匹配.这个问题尚不清楚,所以我猜测规则只是验证了索赔.

在运行时构建自定义规则时,请确保所构建的类实现IRuleProvider.它的IsValid(或其他方法)方法可以动态编译,但会被编译为静态接口,因此您可以从已编译的代码中正常调用它:


The signature of the function should match what you want the dynamic methods to do, obviously. That wasn''t clear from the question so I took a guess that rules just validate a claim.

When you''re building the custom rules at runtime, make sure that the class you''re building implements IRuleProvider. Its IsValid (or whatever) method can be dynamically compiled but it is compiled to a static interface so you can call it normally from the compiled code:

List<IRuleProvider> customRuleProviders = new List<IRuleProvider>;
customRuleProviders.Add(MyMagicRuleProviderCreatingMethod);
...
// some time later
Claim claim = CurrentClaim; // or something
foreach(IRuleProvider customRuleProvider in customRuleProviders)
  if(!customRuleProvider.IsValid(claim)) throw new ArgumentException("Claim not valid");


这篇关于如何创建动态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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