Use reflection to iterate types in all referenced

2019-04-08 21:56发布

问题:

Is there an equivilent to this in silverlight?

 var assemblies = AppDomain.CurrentDomain.GetAssemblies();

http://msdn.microsoft.com/en-us/library/system.appdomain.getassemblies(v=VS.95).aspx

Is there another way of doing this?

Here's a post on how to do it for design time under blend... but how at runtime?

http://joshsmithonwpf.wordpress.com/2010/01/06/the-ultimate-hack-for-silverlight-in-blend/


Doesn't look like this is possible:

Getting Runtime Assemblies in Silverlight 3

http://forums.silverlight.net/p/22050/77847.aspx

http://forums.silverlight.net/t/22050.aspx/1?Get+a+list+of+loaded+assemblies

回答1:

The question originally asked for something equivalent to Assembly.GetExecutingAssembly().GetAvailableTypes() in Silverlight, so that's my first answer.

Silverlight supports reflection:

  • Assembly.GetExecutingAssembly() is supported in Silverlight, including Windows Phone 7 and XBox 360.
  • Assembly.GetExportedTypes() and Assembly.GetTypes() are both supported in Silverlight, including Windows 7 and XBox 360.

See the documentation and select "Silverlight" as the version for details.

Update

  • AppDomain.CurrentDomain is supported in Silverlight 3 and 4.
  • AppDomain.GetAssemblies() is supported in Silverlight 4.

The easiest is to upgrade to Silverlight 4 instead of having to do workarounds.



回答2:

I'm not getting it to work in my SL4 project either, but I tried to cast the the current domain to a dynamic and it works, a dummy workaround until I get VS2010 to recognize that I' using the correct mscorlib.

Dummy workaround:

var loadedAssemblies = ((dynamic)Thread.GetDomain()).GetAssemblies()as Assembly[];