English 中文(简体)
允许C# plugins在申请时进行登记
原标题:Allowing C# plugins register on application hooks

我正在开发一个基于“网络”的应用,希望能够设计一个更加精通和简便的设计。

For the sake of simplicity, the application exposes a set of operations and events:

  • DoSomething()
  • DoOtherThing()
  • OnError
  • OnSuccess

我要向其中一些行动(例如:当事件1起火时, run花1)。

例如:plugin1.HandleError(),On Error.

可以通过订阅活动来做到这一点:

this.OnError += new Plugin1().HandleError();

问题是:

  1. My app doesn t know of the type "Plugin1" (it is a plugin, my app does not reference it directly).
  2. Doing so will instantiate the plugin before time, something i do not want to do.

在“传统”原始模型中,微粒的“客户”负荷,在某些关键点执行原始代码。 例如,在进行某种手术时,图像处理申请。

客户申请知道何时即刻和何时执行原始代码。

在我的申请中,原始内容是决定何时执行(“原始数据应当登记在网上活动”。)

将原始的“执行”法与“登记”法一起保留,这就产生了一个问题,即原DLL公司在登记时将装上记忆,我希望加以防止。

例如,如果在假冒的DLLL中添加一种登记册方法,则原始的DLL必须装上记忆,以便叫做登记册方法。

What could be a good design solution for this particular issue?

  • Lazily loading (or offering lazy/eager loading) of plugin DLLs.
  • Allowing plugins to control which various parts of the system/app they hook into.
问题回答

You are trying to solve a non-existing problem. The mental image of all the types getting loaded when your code calls Assembly.LoadFrom() is wrong. The .NET framework takes full advantage of Windows being a demand-paged virtual memory operating system. And then some.

当你叫LoadFrom(LadFrom)时,球开始滚动。 《刑法》规定,《刑法》规定,《刑法》是核心操作系统摘要。 它更新了内部的界限,以追踪目前居住在上诉法院的集会情况,这是很轻微的。 MMF建立了记忆制图,建立了虚拟记忆页,绘制档案内容的地图。 仅是TLB加工商的一名小笔记官。 没有任何东西从议会档案中读过。

Next you ll use reflection to try to discover a type that implements an interface. That causes the CLR to read some of the assembly metadata from the assembly. At this point, page faults cause the processor to map the content of some of the pages that cover the metadata section of the assembly into RAM. A handful of kilobytes, possibly more if the assembly contains a lot of types.

其次,即时编纂者采取行动,为建筑商制定法典。 这使得加工商将含有IL的建筑商的网页误入RAM。

Etcetera。 核心想法是,组装内容总是在 必要时读。 该机制对于原始产品而言是不同的而不是<>m>,它们与常规大会一样在你们的解决办法中工作。 唯一区别是命令略有不同。 你首先装上大会,然后立即打电话给建筑商。 当时,与在你的法典和《刑法》中称作一类的建筑商相比,《刑法》立即装上了装配。 它需要的时间太长。

What you need to do is to find the path of the dll, and then create an assembly object from it. From there, you will need to get classes you wish to retrieve (for instance, anything that implements your interface):

var assembly = Assembly.Load(AssemblyName.GetAssemblyName(fileFullName));
foreach (Type t in assembly.GetTypes())
{
  if (!typeof(IMyPluginInterface).IsAssignableFrom(t)) continue;
  var instance = Activator.CreateInstance(t) as IMyPluginInterface;
  //Voila, you have lazy loaded the plugin dll and hooked the plugin class to your code
}

当然,从这里讲,你可以自由地做任何希望、使用方法、参加活动等。

装饰板 我倾向于在我的IoC集装箱里装上一台名录(I use StructureMap),尽管你能够按照@Oskar的回答用人工装载。

如果在你提出申请时,你想要支持装饰物,那么“结构”可以是“重新配置的”,从而吸收任何新的gin果。

如果你提出申请,你可以向一辆公共汽车开车。 下面的例子利用结构-Map找到所有登记的活动处理器,但你可以使用便衣旧的反射器或其他IoC集装箱:

public interface IEvent { }
public interface IHandle<TEvent> where TEvent : IEvent {
    void Handle(TEvent e);
}

public static class EventBus {
    public static void RaiseEvent(TEvent e) where TEvent : IEvent {
        foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            handler.Handle(e);
    }
}

之后,你可以提出这样的事件:

public class Foo {  
    public Foo() {
        EventBus.RaiseEvent(new FooCreatedEvent { Created = DateTime.UtcNow });
    }
}

public class FooCreatedEvent : IEvent {
    public DateTime Created {get;set;}
}

并且(例如,在你看来)这样做:

public class FooCreatedEventHandler : IHandle<FooCreatedEvent> {
    public void Handle(FooCreatedEvent e) {
        Logger.Log("Foo created on " + e.Created.ToString());
    }
}

我肯定会建议。 它是我们作为自己“多方管理人员”的基础。

我个人将避免按要求装上议会。 海事组织的启动时间比running申请的用户要等到必须装上必要的原始材料时,要稍长得多(甚至比网上申请上的问题要少一点)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签