Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about typeof().Guid #802

Closed
kant2002 opened this issue Mar 12, 2021 · 5 comments
Closed

Question about typeof().Guid #802

kant2002 opened this issue Mar 12, 2021 · 5 comments
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation question Further information is requested

Comments

@kant2002
Copy link
Contributor

When implementing tests I have approximate code

[ComImport]
[ComVisible(true)]
[Guid("D6DD68D1-86FD-4332-8666-9ABEDEA2D24C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IComInterface
{
    void DoWork();
}

////

internal static Guid IComInterface_GUID = typeof(IComInterface).GUID;

This throws

Unhandled Exception: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.Reflection.MissingMetadataException: 'PInvokeTests.IComInterface' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859
   at System.Reflection.Runtime.TypeInfos.RuntimeBlockedTypeInfo.get_GUID() + 0x42

I was not expecting that this cause any issues. Especially that I have that pattern in other project. Do tests have more aggressive NativeAOT settings then defaults?

@jkotas
Copy link
Member

jkotas commented Mar 12, 2021

Yes, the tests have more restrictive reflection settings than the default.

@jkotas jkotas added area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation question Further information is requested labels Mar 12, 2021
@kant2002
Copy link
Contributor Author

I understand that in general sense it is not possible to have typeof(XXX).GUID properly working, but can compiler ensure that if typeof operates on the well-known type which explicitly has GuidAttribute set, then this expression would be precomputed?

@jkotas
Copy link
Member

jkotas commented Mar 12, 2021

@MichalStrehovsky ?

@MichalStrehovsky
Copy link
Member

at System.Reflection.Runtime.TypeInfos.RuntimeBlockedTypeInfo.get_GUID()

RuntimeBlockedTypeInfo means that reflection blocking is active. The pinvoke test opts into that for its main assembly:

// Make sure the interop data are present even without reflection
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All)]
internal class __BlockAllReflectionAttribute : Attribute { }
}

If you need to do reflection-y things, you either need to add another assembly to the pinvoke test (the attribute is applicable per assembly), or just add a new test.

I understand that in general sense it is not possible to have typeof(XXX).GUID properly working

I think in the general sense this should just work. The problem is opting into reflection blocking. We do that as a size optimization for some of the System.Private assemblies only. ...and in this test.

@jkotas
Copy link
Member

jkotas commented Mar 12, 2021

I think it would be nice to just avoid reflection-y things for the ComWrappers test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants