-
-
Notifications
You must be signed in to change notification settings - Fork 625
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
InvalidOperation Exception importing generic type with a type parameter from declaring type #124
Comments
@jbevain can you comment on this? Would be happy to provide a fix, but would need to understand the rationale behind the new generic type parameter resolution system first. |
Currently at a conference, I'll have a look next week :) |
Hi Jb, did you get a chance to have a look? On Fri, Mar 29, 2013 at 2:30 PM, Jb Evain notifications@github.com wrote:
|
No not yet, I'm just back from intensive traveling :) |
I believe I may be running into this issue as well. I am using ILRepack which depends on Mono.Cecil. I use it to merge a build of google-api-dotnet-client. I am getting the stack trace: The method: has the following state: The subsequent call to: fails with an InvalidOperationException because type.DeclaringType.FullName does not match the value in stack. |
Errors (although same stack) are different: I believe the first one is legit, as the field type argument TValue must be bound to the container class and has no meaning out of it. Hence things work well if you do this instead:
The second one is related to ILRepack renaming of duplicate types (hence the GUID prefix), which prevents the import code to match the type name, I'll try to work on it. |
@gluck: What do you mean with legit? The Exception is legit? I tend towards saying that an import of the field type should automatically import the generic type definition of it's generic class if hasn't already been imported, sticking to the principle of the least surprise. |
If possible, yes I agree, but I'm not fully convinced it is. I believe you still need the context stack to prevent infinite recursion. Note that before this change, Cecil was looking up the generic parameter in the directly enclosing type (ignoring the DeclaringType entirely), no matter if it made sense or not. |
I tried something along the lines of
when importing and it seems to work for now. |
I'll try to have a look some time. |
Just outlining an approach that works for me (and I have a very special use case for .Import as I only need it to generate stub TypeReferences for testing some code that uses cecil over real assemblies). The code posted above is used in my Module.Import wrapper, so no modifications to cecil itself. |
This is 6 years old, closing this. Thanks! |
Using module.Import( typeof( HasStringKeyedDictionary<> ).GetField( "PARTIALLY_CLOSED" ).FieldType ) throws an InvalidOperationException.
The error was introduced with one of the last change to ImportGenericContext and I used to get it to import properly before that (not sure if I have to blame f5a7557 or 4f76389).
The problem seems to be that the declaring type of the parameter TValue doesn't end up on the stack that ImportGenericContext wraps. I don't quite understand why that stack is necessary in the first place and what benefit it provides.
I agree with the notion that we have "scopes" when resolving generic type parameters (their scope is their declaring type) and these may be nested but System.Reflection gives you the right declaring type anyway so I don't see a reason we need the stack even though scopes may be nested and shadow equally named parameters.
The text was updated successfully, but these errors were encountered: