Hi,

I will paste here our code where I think it is the problem, and try to
explain a little bit. I will be glad to explain it with further
details if necessary.

In my code I look for some specific method references in the assembly
and then try to create a new method reference which has the same
signature but with a different method name. The purpose is to
substitute in the operand of the instruction the original method
reference with the one I have just created.

When I find a method reference to substitute, I call
FixMethodReference passing the new method name, the method reference
found and the module where I want to create the new reference. The
module normally is the same as the one where the original reference
is. The return value is the new method reference.

When I apply this code to the method System.Boolean
Namespace.Class::ListIsNullOrEmpty<T>(System.Collections.Generic.List`1<T>),
I see that I miss the generic parameter name in
System.Collections.Generic.List`1<T> after calling
AssemblyDefinition.Write. This parameter of type
System.Collections.Generic.List`1<T> is created in the foreach
(ParameterDefinition parameter in methodReference.Parameters) in
FixMethodReference method. The new ParameterDefinition takes its type
from a call to FixTypeReference, which receives the
System.Collections.Generic.List`1<T> GenericInstanceType and a
reference to the main module. In FixTypeReference we redirect the call
to FixGenericTypeReference, where the generic arguments and parameters
are just copied from the original collection. Could this be the error
and should I create new generic arguments and parameters cloned from
the original ones?

Sorry if the question is a bit silly, I am really a newbie in the IL
level. Here is the code I am referring to.

private MethodReference FixMethodReference(string actualMethodName,
MethodReference methodReference, ModuleDefinition module)
        {
            if (methodReference is GenericInstanceMethod)
                return FixGenericMethodReference(actualMethodName,
methodReference as GenericInstanceMethod, module);
            MethodDefinition foundMethod = null;
            TypeDefinition foundType =
LookForIdenticalType(methodReference.DeclaringType, module.Types);
            if (foundType != null)
            {
                foundMethod = LookForIdenticalMethod(actualMethodName,
methodReference, foundType.Methods);
                if (foundMethod == null)
                    foundMethod =
LookForIdenticalMethod(methodReference.Name, methodReference,
foundType.Methods);
            }
            MethodReference fixedReference = new
MethodReference(foundMethod != null ? actualMethodName :
methodReference.Name, FixTypeReference(methodReference.ReturnType,
module));
            fixedReference.HasThis = methodReference.HasThis;
            fixedReference.DeclaringType =
FixTypeReference(methodReference.DeclaringType, module);
            foreach (ParameterDefinition parameter in
methodReference.Parameters)
                fixedReference.Parameters.Add(new
ParameterDefinition(parameter.Name, parameter.Attributes,
FixTypeReference(parameter.ParameterType, module)));
            return fixedReference;
        }

        private TypeReference FixTypeReference(TypeReference
typeReference,  ModuleDefinition module)
        {
            if (typeReference is GenericInstanceType)
                return FixGenericTypeReference(typeReference as
GenericInstanceType, module);
            TypeReference fixedReference =
LookForIdenticalType(typeReference, module.Types);
            if (fixedReference != null)
                return fixedReference;
            return module.Import(typeReference);
        }

        private GenericInstanceType
FixGenericTypeReference(GenericInstanceType genericTypeReference,
ModuleDefinition module)
        {
            TypeReference fixedElementType =
FixTypeReference(genericTypeReference.ElementType, module);
            GenericInstanceType fixedGenericType = new
GenericInstanceType(fixedElementType);
            foreach (TypeReference genericArgument in
genericTypeReference.GenericArguments)
 
fixedGenericType.GenericArguments.Add(genericArgument);
            foreach (GenericParameter genericParameter in
genericTypeReference.GenericParameters)
 
fixedGenericType.GenericParameters.Add(genericParameter);
            return fixedGenericType;
        }

        private GenericInstanceMethod FixGenericMethodReference(string
actualMethodName, GenericInstanceMethod genericMethodReference,
ModuleDefinition module)
        {
            MethodReference fixedElementMethod =
FixMethodReference(actualMethodName,
genericMethodReference.ElementMethod, module);
            GenericInstanceMethod fixedGenericMethod = new
GenericInstanceMethod(fixedElementMethod);
            foreach (TypeReference genericArgument in
genericMethodReference.GenericArguments)
 
fixedGenericMethod.GenericArguments.Add(genericArgument);
            foreach (GenericParameter genericParameter in
genericMethodReference.GenericParameters)
 
fixedGenericMethod.GenericParameters.Add(genericParameter);
            return fixedGenericMethod;
        }

Many thanks in advance for your help and attention.

Best regards,

Jose Antonio


On 13 jun, 09:43, Jb Evain <[email protected]> wrote:
> Hi,
>
> On Mon, Jun 13, 2011 at 9:06 AM, arrowop <[email protected]> wrote:
> > Thanks a lot for your quick response. In my case I am using the 0.9.4
> > version downloaded from the Downloads section of github.
>
> No, please use the tip of master.
>
> > About the issue I am experiencing, actually I am not just reading and
> > writing the method reference, I create a new method reference from an
> > existing one, so I am pretty sure that I am doing something wrong.
>
> Show us some code then?
>
> Jb

-- 
--
mono-cecil

Reply via email to