Assembly.GetExecutingAssembly doesn't exist in PCL

扶醉桌前 提交于 2019-12-01 15:05:36

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.

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

It is crucial here to understand that the Portable Class Library / PLC 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!