Strange behavior of the MethodReference.Resolve() method

47 views
Skip to first unread message

digitalio Q

unread,
Feb 4, 2020, 6:59:55 PM2/4/20
to mono-cecil
Executing the following code, all the Resolve() methods works properly but the last one; any idea why it fails?

            var resolver = new DefaultAssemblyResolver();

            var asm = resolver.Resolve(new AssemblyNameReference("ClassLibrary1", new Version()));
            var module = asm.MainModule;
            var sysAsm = module.AssemblyReferences.First(cur => cur.Name == "netstandard");

            // way 1
            var ctorSource = typeof(SerializableAttribute).GetConstructor(Type.EmptyTypes);
            var ctorRef1 = module.ImportReference(ctorSource);
            var ctorDef1 = ctorRef1.Resolve();
            if (ctorDef1 == null)
                throw new ApplicationException(); // doesn't throw

            // way 2
            var returnType = new TypeReference("System", "Void", module, sysAsm);
            var returnTypeDef = returnType.Resolve();
            if (returnTypeDef == null)
                throw new ApplicationException();

            var attribType = new TypeReference("System", "SerializableAttribute", module, sysAsm);
            var attribTypeDef = attribType.Resolve();
            if (attribTypeDef == null)
                throw new ApplicationException();

            var ctorRef2 = new MethodReference(".ctor", returnType, attribType)
            {
                HasThis = true,
            };
            ctorRef2 = module.ImportReference(ctorRef2);
            var ctorDef2 = ctorRef2.Resolve();
            if (ctorDef2 == null)
                throw new ApplicationException(); // throws


Jb Evain

unread,
Feb 4, 2020, 7:03:46 PM2/4/20
to mono-...@googlegroups.com
Hi,

This is very likely to be because System.Void is a special type and our Resolver ends up being confused.

You can use the module TypeSystem to get a reference to System.Void.

Jb

--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups "mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mono-cecil+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mono-cecil/14b921f6-26de-456c-a9e9-b088f7ee12ae%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages