.net - How to load an assembly as reflection-only in a new AppDomain? -
i'm experienced in c# relatively unfamiliar concepts of appdomain , like. anyway, i'm trying assembly load in reflection-only context can grab of namespaces. here code have right (warning: powershell):
function get-namespaces($assembly) { $assemblyclass = [reflection.assembly] $winmdclass = [runtime.interopservices.windowsruntime.windowsruntimemetadata] $domain = [appdomain]::currentdomain # since desktop .net can't work winmd files, # have use reflection-only apis , preload # dependencies manually. $appdomainhandler = { param($sender, $e); $assemblyclass::reflectiononlyload($e.name) } $winmdhandler = { param($sender, $e) [string[]] $empty = @() $path = $winmdclass::resolvenamespace($e.namespacename, $empty) | select -index 0 $e.resolvedassemblies.add($assemblyclass::reflectiononlyloadfrom($path)) } # hook handlers $domain.add_reflectiononlyassemblyresolve($appdomainhandler) $winmdclass::add_reflectiononlynamespaceresolve($winmdhandler) # actual work $assemblyobject = $assemblyclass::reflectiononlyloadfrom($assembly) $types = $assemblyobject.gettypes() $namespaces = $types | ? ispublic | select namespace -unique # deregister handlers $domain.remove_reflectiononlyassemblyresolve($appdomainhandler) $winmdclass::remove_reflectiononlynamespaceresolve($winmdhandler) return $namespaces } for reason, when i'm running function on assemblies system.xaml or windowsbase i'm getting these errors:
exception calling "reflectiononlyloadfrom" "1" argument(s): "api restriction: assembly 'file:///c:\program files (x86)\reference assemblies\microsoft\ framework\.netframework\v4.6.1\system.xaml.dll' has loaded different location. cannot loaded new location within same appdomain." @ c:\users\james\code\csharp\shims.xaml\generate.ps1:50 char:5 + $assemblyobject = $assemblyclass::reflectiononlyloadfrom($assembl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : notspecified: (:) [], methodinvocationexception + fullyqualifiederrorid : fileloadexception
so i'd know is, how can load assembly in new appdomain? checked msdn can find methods createinstanceandunwrap, can't/don't want since reflection-only.
tl;dr: how can load assemblies in reflection-only context in new appdomain? both c# , powershell code samples welcome.
edit: here github gist of script i've made, others can repro error/test changes.
i think i've got it:
function load-assemblyinnewappdomain($assembly) { $domain = [appdomain]::createdomain("foo") $domain.docallback ({ $loaded = [reflection.assembly]::load($assembly) }) }
Comments
Post a Comment