-
Notifications
You must be signed in to change notification settings - Fork 3
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
Shim throws TypeInitializationException in Unity Engine #37
Comments
This might occur due to a misconfiguration in my project, leading to the dependency System.Reflection.Emit.Lightweight missing. Will investigate further. |
I will test it out in the sandbox just to be sure. UPDATE: So far the below sample executes without exception in the Sandbox. Sample with target framework net48namespace Pose.Sandbox
{
public static class SimpleStaticClass
{
public static double SimpleStaticMethod()
{
return 5;
}
}
public class Program
{
public static async Task<int> GetIntAsync()
{
Console.WriteLine("Here");
return await Task.FromResult(1);
}
public static void Main(string[] args)
{
Shim methodShim = Shim.Replace(() => SimpleStaticClass.SimpleStaticMethod()).With(
() => (double)20);
PoseContext.Isolate( () =>
{
//doing nothing yet
}, methodShim);
}
}
} |
@sam-ln: Let me know what you find out. Perhaps there is some way we can guard against this. |
So, the issue doesn't seem to be related to a missing dependency. I can use System.Reflection.Emit.Lightweight.DynamicMethod in the project just fine. The Expection |
Oh, I see, it's a private method. pose/src/Pose/Helpers/StubHelper.cs Lines 10 to 14 in 91ac854
In this static initializer you check whether DynamicMethod has a method GetMethodDescriptor and there it throws an Exception because for some reason it can't access it.
|
It seems that |
@sam-ln That's weird because we have a test for specifically this scenario. Please see
@sam-ln You mentioned that methods cannot be retrieved in a lot of environments. Could it be that this is a Unity specific issue? |
I think this only applies for [SecurityCritical] methods. The article I linked says
I'm not entirely sure what "transparent" refers to in this context, but I think that this might be the issue. The .NET environment in Unity (running on Mono) seems to be affected by this restriction, but I'd be surprised if this problem is only Unity-specific. |
@Miista Sure, I could set up a unity project with your test suite in it if that would help. I guess the easiest way to check for that restriction would be to execute var methods = typeof(DynamicMethod).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); If I run it in a .NET Core solution it shows all methods including |
@sam-ln What should we do if the method does not exist (as is the case for Unity)? Do we provide a better exception message? Scripting restrictions for Unity: https://docs.unity3d.com/Manual/ScriptingRestrictions.html I'm reading through the above link and I spot this:
On the same page I can see that Mono does not support ahead-of-time. Could this be the reason why it fails? That said, I can see that Mono source: https://github.com/mono/mono/blob/main/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs The method does exist in the reference source: https://github.com/mono/mono/blob/38b0227c1ce0c53058a5d78d080923435132773a/mcs/class/referencesource/mscorlib/system/reflection/emit/dynamicmethod.cs#L580 We could consider copying the implementation verbatim and provide it ourselves if we find that there is none provided by the platform. Although I'm not really keen on writing this ourselves. |
@Miista Mono is not used for the AOT platforms (They use IL2CCP for that), so that shouldn't be the issue. |
@sam-ln Have you had a look at managed code stripping? Link: https://docs.unity3d.com/Manual/ManagedCodeStripping.html It looks like it can be disabled by declaring a Link.xml. Ref: https://docs.unity3d.com/Manual/ManagedCodeStripping.html#LinkXMLAnnotation I'll be honest with you. I haven't worked with Unity before. |
@sam-ln Have you had a chance to look more into this? |
@Miista Good idea regarding the code stripping, this wasn't the issue though (it's disabled in my project). |
No worries! If you'd like to look into this more, I've set up a demo project for you with Poser imported and some Unit test cases which highlight the issue. Let me know if you need any assistance! Right now, my best guess is that the problem is the missing implementation of |
@sam-ln Did you find a solution or a suitable workaround at least? |
@Miista Unfortunately not, but I haven't tried since. I think where we left off is that we could write a GetMethodDescriptor replacement for it to work in Mono. Have you had a chance to open it up in Unity and review the problem yourself? |
When trying to shim a static method, the execution of PoseContext.Isolate fails with a TypeInitializationException.
The class whose method I'm trying to shim:
My test-class:
The stack-trace:
More details:
The project is using the net48 Pose dll.
It is run from within Unity Engine
Issue appears in both 2.0 and 2.1 alpha
BTW: @Miista Thanks a lot for reviving this project! 👍
The text was updated successfully, but these errors were encountered: