vb.net PCL 中不存在 Assembly.GetExecutingAssembly

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19718232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:40:53  来源:igfitidea点击:

Assembly.GetExecutingAssembly doesn't exist in PCL

.netvb.netreflectionportable-class-library

提问by Todd Main

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

我在 VB、VS2012 中设置了一个 PCL,并将其设置为 Profile 78(WinRT、WinPhone8、.NET 4.5)。我没有GetExecutingAssembly可用的Assembly。根据这个,应该是提供给PCLS。唯一可用的方法是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 GetExecutingAssemblyother than Imports System.Reflection? Any other ideas?

有没有人我应该怎么做?例如,这是真的,我的环境搞砸了,有另一种方式来获得GetExecutingAssembly比其他Imports System.Reflection?还有其他想法吗?

回答by Daniel Plaisted

In general, you should use something like typeof(MyType).GetTypeInfo().Assemblyinstead 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.

一般来说,你应该使用类似的东西typeof(MyType).GetTypeInfo().Assembly而不是Assembly.GetExecutingAssembly(). GetExecutingAssembly 必须基本上检查调用堆栈以找出调用它的方法,然后查找相应的程序集。如果方法曾经跨程序集边界内联,这可能会中断,这就是 GetExecutingAssembly 方法不在 Profile 78(以及适用于 Windows 应用商店应用程序的 .NET)使用的“新”反射表面区域的原因。

回答by Lorenz Lo Sauer

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

这种分离在 PLC 中确实深入且非常细致。

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

在这里理解作为平台配置文件的可移植类库/PLC不存在是至关重要的。在编译您的 PLC 项目时,正在运行的应用程序不会受到与编译器相同的限制。

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.

这种方法只会在沙盒程序集环境中为您生成可访问的程序集。但它为您提供了如何访问您不应该访问的“东西”的起点。