简体   繁体   English

如何在C#中使用反射实现钩子?

[英]How can I implement hooks using reflection in C#?

For my own amusement and to learn the C# reflection APIs, I've been toying with the idea of an useless application built completely out of plugins, such that the only thing done by the main program is to read a config file, and load a plugin that will proceed to load every other assembly it's been configured to load on the fly. 为了我自己的娱乐和学习C#反射API,我一直在玩一个完全由插件构建的无用应用程序的想法,这样主程序唯一要做的就是读取配置文件并加载一个该插件将继续加载其他已配置为即时加载的程序集。

To do this, I was thinking of using attributes to define (...)services(?) and events (for the sake of argument, let's call them ServiceAttribute(string) EventAttribute(string), where the parameter is the name of the hook), and then keep a table of where we can find these. 为此,我正在考虑使用属性来定义(...)services(?)和事件(出于参数的考虑,我们称它们为ServiceAttribute(string)EventAttribute(string),其中参数是钩子),然后保留可在其中找到这些表的表。 An example would be like this 一个例子就是这样

namespace example{
  public class Plugin : IPlugin{
    [Service("myService")]
    public void PrintFrickingEverything(params string[] toPrint){
      foreach(string s in toPrint){
        Console.WriteLine(s);
      }
    }
  }
}

And the hook would be accessed somehow by asking for "example.Plugin.myService" 并通过询问“ example.Plugin.myService”以某种方式访问​​该钩子

However, a couple of things stick in my head, 但是,有几件事仍然困扰着我,

one: this implementation seems to be Martin Fowler's service locator pattern, which I think it couples things too tightly together, and would rather avoid it if possible. 一:此实现似乎是马丁·福勒(Martin Fowler)的服务定位器模式,我认为该模式将事情紧密地结合在一起,如果可能的话,宁愿避免使用。

two: While I've done something similar to make a dispatcher for plugins in PHP, the type safety in C# makes this approach difficult. 第二:虽然我做了一些类似的事情来为PHP中的插件创建调度程序,但是C#中的类型安全性使这种方法变得困难。

I know I could use MS's library for this kind of thing, but I want to build this from the ground up by myself so I can learn about it along way to help me with other things I want to write later on. 我知道我可以使用MS的库来做这种事情,但是我想自己重新构建它,这样我就可以在以后帮助我做其他事情的基础上学习它。

Anyway, tl;dr: I want to get past the typing system somehow and be able to keep these methods somewhere, yet still be able to call them without needing to resort to casting an array of objects. tl; dr:无论如何,我想以某种方式超越打字系统,并且能够将这些方法保留在某处,但仍然能够调用它们而无需诉诸转换对象数组。 Is it possible? 可能吗?

YES. 是。 What you are trying to do is how we did plugins years ago, and it works okay. 您要尝试做的是几年前我们做插件的方式,而且效果还不错。 Nowadays we have MEF to do it, however to learn I recommed taking a look at this: http://www.codeproject.com/Articles/6334/Plug-ins-in-C 如今,我们有MEF来做,但是为了学习,我建议您看一下: http : //www.codeproject.com/Articles/6334/Plug-ins-in-C

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

相关问题 如何在C#中使用Reflection创建类对象? - how can I create Class object using Reflection in c#? C#,如何使用反射添加FluentValidation实例? - In C#, how can I add instances of FluentValidation using reflection? 如何在C#中通过反射实现接口 - how to implement interface through reflection in c# 我可以使用反射更改C#中的私有只读字段吗? - Can I change a private readonly field in C# using reflection? 我可以在C#中使用反射在运行时创建单元测试吗? - Can I create a unit test at runtime in C# using reflection? 如何在C#中使用反射从类型和设置属性值中按名称获取属性 - How can I Get the property by name from type and Set Propert value using reflection in C# 如何使用反射分析C#中的特殊约束? - How can I analyse special kind of contraints in C# using reflection? 如何在C#中使用反射从类中获取名称的表单引用? - How can I get a form reference from a class by name using reflection in C#? 如何使用反射获取C#静态类属性的名称? - How can I get the name of a C# static class property using reflection? 如何提示 C# 8.0 可为空引用系统使用反射初始化属性 - How can I hint the C# 8.0 nullable reference system that a property is initalized using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM