示例#1
0
 private MethodDefinition ConvertConstructorToGenericInstanceMethod(MethodDefinition constructor, TypeReference declaringType)
 {
     if (!declaringType.IsGenericInstance)
     {
         return(constructor);
     }
     else
     {
         GenericInstanceType type = (GenericInstanceType)declaringType;
         if (type.HasGenericArguments)
         {
             MethodAttributes attributes = MethodAttributes.RTSpecialName
                                           | MethodAttributes.SpecialName
                                           | MethodAttributes.Assembly;
             MethodDefinition newConstructor = new MethodDefinition(".ctor", attributes, InstanceOfTypeSystem.Void);
             foreach (ParameterDefinition param in constructor.Parameters)
             {
                 if (param.ParameterType.IsGenericParameter)
                 {
                     int indexOfGenericType = 0;
                     foreach (GenericParameter genericParameter in type.ElementType.GenericParameters)
                     {
                         if (genericParameter.Name == param.ParameterType.Name)
                         {
                             break;
                         }
                         else
                         {
                             indexOfGenericType++;
                         }
                     }
                     if (indexOfGenericType < type.ElementType.GenericParameters.Count)
                     {
                         newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, type.GenericArguments[indexOfGenericType]));
                     }
                     else
                     {
                         // this should not happen and will create an error in the generated code.
                         newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, InstanceOfTypeSystem.Void));
                     }
                 }
                 else if (param.ParameterType.HasGenericParameters)
                 {
                     newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, AnalysisUtils.MakeInstanceTypeFromTypeReference(param.ParameterType, type.GenericArguments.ToArray())));
                 }
                 else if (param.ParameterType.IsGenericInstance)
                 {
                     TypeReference paramElementType = ((GenericInstanceType)param.ParameterType).ElementType;
                     if (paramElementType.HasGenericParameters)
                     {
                         newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, AnalysisUtils.MakeInstanceTypeFromTypeReference(paramElementType, type.GenericArguments.ToArray())));
                     }
                     else
                     {
                         newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, param.ParameterType));
                     }
                 }
                 else
                 {
                     newConstructor.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, param.ParameterType));
                 }
             }
             return(newConstructor);
         }
         else
         {
             return(constructor);
         }
     }
 }