ReflectionOnlyLoad can it be garbage collected?

2019-09-13 18:19发布

I want to "hot" load some pre-packaged assembli(es) into a separate AppDomain, the thing however is I do not know the name of the entry point class or even the assembly file. I need to find this entry point so I can run some initialization routine.

So what I intend to do is to run ReflectionOnlyLoad on all the files and find the one that follows a certain convention ie. annotated/implements a certain interface etc.

Question is, will I start leaking memory if I were to run ReflectionOnlyLoad from the main AppDomain over and over? If this can't be run from the main app domain, what are my options, because again I do not know where the entry point is.

Also any additional information about the subtleties in using ReflectionOnlyLoad is appreciated.

3条回答
太酷不给撩
2楼-- · 2019-09-13 18:46

ReflectionOnlyLoad won't solve your problem, see docs

Why don't you execute the code for finding the entry point etc. in the new AppDomain?

查看更多
时光不老,我们不散
3楼-- · 2019-09-13 18:50

Cannot reflect through the dlls. Even with reflection only load, the type sticks to the main AppDomain.

2 Solutions:

  1. Put the entry point in an xml somewhere and parse that.
  2. Use a 2 stage AppDomain, one for the reflector, and then another for the actual object.

I picked (1) since it's the most sensible.

(2) I have to pass through 2 separate proxies in order to issue command to the actual remote object, that or I need to couple the interfaces much more closely than I like. Not to mention being a pain to code.

查看更多
Lonely孤独者°
4楼-- · 2019-09-13 18:51

I recommend Mono.Cecil. It's a simple assembly you can use on .net (it doesn't require the Mono runtime). It offers an API to load assemblies as data, and works pretty well. I found the API easy to work with, and it suffered from none of the problems I experienced when using reflection-only-load.

You can also use CCI, which is an open source project by MS that offers an assembly reader.

See also: CCI vs. Mono.Cecil -- advantages and disadvantages

查看更多
登录 后发表回答