Assembly.GetExecutingAssembly doesn't exist in PCL
Asked Answered
L

2

15

I set up a PCL in VB, VS2012 and set it for Profile 78 (WinRT, WinPhone8, .NET 4.5). I don't have GetExecutingAssembly available on Assembly. According to this, it should be available to PCLs. The only method available is Assembly.Load().

Does anyone what I should do with this? E.g. is this true, is my environment screwed up, is there another way to access GetExecutingAssembly other than Imports System.Reflection? Any other ideas?

Lobell answered 31/10, 2013 at 23:14 Comment(3)
It is available, target Silverlight and Desktop for example. WinRT or Phone, no.Butanol
Need Async/Await, so can't target SL. Don't need SL.Lobell
This SO answer only concerns GetExecutingAssembly() for Windows Store apps, but it might be useful nevertheless.Clamp
C
29

In general, you should use something like typeof(MyType).GetTypeInfo().Assembly instead of Assembly.GetExecutingAssembly(). GetExecutingAssembly has to basically examine the call stack to figure out what method is calling it and then look up the corresponding assembly. This could break if methods are ever inlined across assembly boundaries, which is why the GetExecutingAssembly method isn't in the "new" reflection surface area which Profile 78 (as well as .NET for Windows Store apps) uses.

Caddoan answered 7/11, 2013 at 19:42 Comment(0)
J
2

The separation runs indeed deep and quite meticulously within the PCL.

It is crucial here to understand that the Portable Class Library as a platform profile doesn't exist. The running application will not incur the same restrictions as the compiler does, upon compiling your PLC project.

Here is one way to break through the barrier:

using System;
...
try {
    var getExecutingAssembly = typeof(Assembly).GetRuntimeMethods()
                                .Where(m => m.Name.Equals("GetExecutingAssembly"))
                                .FirstOrDefault();
    var assemblies = getExecutingAssembly.Invoke(null, null);
} catch(Exception exc){
   ... try something else
} finally{
   ... time for some alternative 
}

This approach will only yield you the accessible assemblies within the sandboxed assembly environment. But it gives you a starting point on how to access the "stuff" you are not supposed to.

Jellyfish answered 28/8, 2016 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.