Assembly.GetCallingAssembly不支持WinRT的,但对于便携式类库?不支持、类库、Assembly、GetCallingAssembly

2023-09-03 16:25:03 作者:文愛

我需要获得有关它要求我的组件装配的一些元数据。因此,使用 Assembly.GetCallingAssembly()似乎是天作之合。然而,我发现它的工作原理无处不在,除了在Windows应用商店。在那里的支持:

I'm needing to get some meta-data about the assembly which calls my component. As such, using Assembly.GetCallingAssembly() seems to be a natural fit. However, I've found it works everywhere except for in Windows Store. Where it's supported:

Phone 7.0+ 在NET 1.0 + Portable类库 Phone 7.0+ .Net 1.0+ Portable Class Libraries

然而,如果它不支持直接在Windows Store应用程序中。我可以做一个便携式类库,然后把它从那里的Windows Store应用程序内部的,但我不能只是把它直接在Windows应用商店的应用程序/类库。

However, where it's not supported is directly within a Windows Store application. I can make a portable class library and then call it from there inside of a Windows Store application, but I can't just put it directly in the Windows Store app/class library.

有没有一种解决方法或其他方式获得通过组装提供的元数据的类型?

Is there a workaround for this or some other way to get the type of metadata provided by Assembly?

推荐答案

Assembly.GetCallingAssembly 不暴露在WinRT中 - 据说是因为它的语义是不可靠的内联的脸上等(来源),但它也并不适合过多在Windows应用商店的应用程序所允许的限制反映。你可以像 Assembly.GetCurrentAssembly(),比如像这样的:

Assembly.GetCallingAssembly is not exposed in WinRT - supposedly because its semantics are unreliable in the face of inlining etc (source), but it also doesn't fit too much with the restricted reflection permitted in Windows Store apps. You can get something like Assembly.GetCurrentAssembly(), eg like this:

typeof(MainPage).GetTypeInfo().Assembly

但是,这不一样的。与受限反射模型,获得堆栈跟踪在运行时有可能在.NET将不可能要么

But that's not the same at all. With the restricted reflection model, obtaining a stack trace at runtime as is possible in .NET will not be possible either.

对于便携式类库,我正要说 Assembly.GetCurrentAssembly()支持在一般的便携式类库,但只是没有在WinRT中 - 其中才有意义,如果它根本就没有在该平台上。但实际上,它似乎是present在所有配置文件,包括WinRT的除外的WinRT + .NET4.5 - 它似乎还有一定在这里与此不一致某种监督。因此,该方法的是的present在WinRT中(而且没有排序重定向正在进行中),但不可见的元数据可以在编译的时候。

As to the portable class library, I was about to say that Assembly.GetCurrentAssembly() is supported in portable class libraries in general, but just not in WinRT - which would make sense if it's simply not in that platform. But actually, it seems that it is present in all profiles including WinRT except WinRT+.NET4.5 - it seems there must be some sort of oversight here with this inconsistency. So the method is present in WinRT (and moreover there's no sort of redirection going on), but not visible in the metadata available at compile time.

因此​​,您可以调用该方法与反思:

Therefore you can call the method with reflection:

var assembly = (Assembly) typeof(Assembly).GetTypeInfo()
    .GetDeclaredMethod("GetCallingAssembly")
    .Invoke(null, new object[0]);

我presume非可视性这种方法在Windows应用商店的应用程序是一个我们希望这将消失。

I presume the non-visibility of this method in Windows Store apps is a "we wish this would go away".

(这个答案只涉及我可以而不是我应该)。

(This answer only concerns "can I" not "should I").