I finally solved it myself. The code posted for creating generic
methods did not work for me. Here is what does work for me:

(this is for my special case, the non generic arguments have to be add
to this method if needed)


        public static MethodReference MakeGeneric(this MethodReference
aMethod, params TypeReference[] arguments)
        {
            //WARNING, aMethod plus aMethod.DeclaringType type must
have been imported before!
            var _GenericType =
aMethod.DeclaringType.MakeGenericType(arguments);
            var _GenericTypeReference =
aMethod.DeclaringType.Module.Import(_GenericType);
            GenericParameter _GenericParameter = new
GenericParameter(_GenericType);
 
_GenericTypeReference.GenericParameters.Add(_GenericParameter);
            MethodReference _MethodReference = new
MethodReference(aMethod.Name, aMethod.ReturnType)
            {
                DeclaringType = _GenericTypeReference,
                HasThis = aMethod.HasThis,
                ExplicitThis = aMethod.ExplicitThis,
                CallingConvention = aMethod.CallingConvention,
            };
            foreach (GenericParameter _GenericParameter2 in
_GenericTypeReference.GenericParameters)
            {
                ParameterDefinition _ParameterDefintion = new
ParameterDefinition(_GenericParameter2);
                _MethodReference.Parameters.Add(_ParameterDefintion);
            }
            _MethodReference =
aMethod.DeclaringType.Module.Import(_MethodReference);
            return _MethodReference;
        }

On Nov 18, 10:44 am, joer <[email protected]> wrote:
> Hi JB,
>
> well, did some more tests, and below is what I have now. As far as I
> understand, in my cases I CANNOT call MakeGenericMethod, because my
> method is actually a NON generic method of a Generic type, just
> expecting the Generic type parameter as the argument. Here is where
> the problem lies.
>
> The code so far:
>
> _LINQ = _Resolver.Resolve(_AssemblyName);//is the name of
> System.Data.Linq
> var _EntityRefClassLocal = _LINQ.MainModule.Types.Where(x => x.Name ==
> "EntityRef`1").FirstOrDefault();
> var _EntityRefClass =
> assemblyToEnhance.MainModule.Import(_EntityRefClassLocal);
> var _entityRefClassSetEntityMethod =
> EntityRefClass.Resolve().GetMethod("set_Entity");
> var _EntityRefGenericMethod =
> entityRefClassSetEntityMethod.MakeGeneric(_Field.FieldType); //
> FieldType is OL2S.TestClassesIntercepted.Party
>
> // method set_Entity is EntityRef<T>::set_Entity(T)  NOT !!
> EntityRef<T>::set_Entity<T>(someargument)
> // so GenericParameters.Count is 0, therefore I cannot call
> MakeGenericMethod
> // but there is an ARGUMENT which is currently nothing, so I tried to
> set this
>
> _EntityRefGenericMethod.Parameters[0] = new
> ParameterDefinition(_Field.FieldType); //FieldType is
> OL2S.TestClassesIntercepted.Party
>
> //Import it
>
> MethodReference _MethodReference =
> aMSILStream.DeclaringType.Module.Import(_EntityRefGenericMethod);
>
> //and call the method
>
> _NewInstruction = Instruction.Create(OpCodes.Call, _MethodReference);
>
> // MethodReference  to string no shows this as
>
> instance void valuetype
> [System.Data.Linq]System.Data.Linq.EntityRef`1<OL2S.TestClassesIntercepted.Party>::set_Entity(OL2S.TestClassesIntercepted.Party)
>
> but it should be
>
> instance void valuetype
> [System.Data.Linq]System.Data.Linq.EntityRef`1<OL2S.TestClassesIntercepted.Party>::set_Entity(!
> 0)
>
> and ILDASM shows:
>
> call       instance class
> [TestClassesIntercepted]OL2S.TestClassesIntercepted.Party valuetype
> [System.Data.Linq]System.Data.Linq.EntityRef`1<class
> [TestClassesIntercepted]OL2S.TestClassesIntercepted.Party>::set_Entity<[1]>( 
> [SIGNATURE
> ENDED PREMATURELY])
>
> It suddenly seems to have a return type, although the MethodReference
> shows systmem.void.
>
> Thanks for the quick help, let me know if you need anything more.
>
> Joe Robe
>
> On Nov 18, 10:08 am, Jb Evain <[email protected]> wrote:
>
> > Hey,
>
> > On Thu, Nov 18, 2010 at 3:56 PM, joer <[email protected]> wrote:
> > > What am I doing wrong ?
>
> > It's hard to tell without seeing the actual source.
>
> > Jb
>
>

-- 
--
mono-cecil

Reply via email to