static public TypeReference GetRealType(this MethodReference method, TypeReference type)
 {
     if (type.Name.StartsWith("!!"))
     {
         if (method.GenericParameters.Count > 0)
         {
             return(method.GenericParameters[int.Parse(type.Name.Substring(2))]);
         }
         return(method.Resolve().GenericParameters[int.Parse(type.Name.Substring(2))]);
     }
     if (type.Name.StartsWith("!"))
     {
         if (method.DeclaringType.GenericParameters.Count > 0)
         {
             return(method.DeclaringType.GenericParameters[int.Parse(type.Name.Substring(1))]);
         }
         return(method.DeclaringType.Resolve().GenericParameters[int.Parse(type.Name.Substring(1))]);
     }
     if (type.IsGenericParameter)
     {
         return(method.Resolve().GenericParameterTypes().Reverse().First(_Type => _Type.Name == type.Name));
     }
     if (type.GenericParameters.Count > 0)
     {
         return(type.MakeGenericType(type.GenericParameters.Select(_Type => method.GetRealType(_Type))));
     }
     if (type is GenericInstanceType)
     {
         return(method.Module.Import(type).MakeGenericType((type as GenericInstanceType).GenericArguments.Select(_Type => method.GetRealType(_Type))));
     }
     return(type);
 }
 public CecilGenericSerializerFactory(Type genericSerializableType, TypeReference genericSerializerType)
     : this(genericSerializableType, (type => genericSerializerType.MakeGenericType(((GenericInstanceType)type).GenericArguments.ToArray())))
 {
     if (genericSerializerType == null)
         throw new ArgumentNullException("genericSerializerType");
     if (genericSerializableType == null)
         throw new ArgumentNullException("genericSerializableType");
 }
Beispiel #3
0
        //static public int Emit(this MethodBody body, MethodReference method)
        //{
        //    body.Emit(OpCodes.Ldtoken, method);
        //    //body.Emit(OpCodes.Ldtoken, method.DeclaringType);
        //    if (method.DeclaringType.GenericParameters.Count == 0) { body.Emit(OpCodes.Ldtoken, method.DeclaringType); }
        //    else { body.Emit(OpCodes.Ldtoken, method.DeclaringType.MakeGenericType(method.DeclaringType.GenericParameters)); }
        //    return body.Emit(OpCodes.Call, __MethodBody.GetMethodFromHandle);
        //}

        static public TypeReference Resolve(this MethodDefinition method, TypeReference type)
        {
            if (type.GenericParameters.Count > 0)
            {
                return(type.MakeGenericType(type.GenericParameters.Select(_Type => method.Resolve(_Type))));
            }
            if (type is GenericInstanceType)
            {
                var g = method.Module.Import(type.Resolve()).MakeGenericType((type as GenericInstanceType).GenericArguments.Select(_Type => method.Resolve(_Type)));
                return(g);
            }
            return(type.IsGenericParameter ? method.GenericParameterType(type.Name) : type);
        }