예제 #1
0
 private CodeTypeMember GetCodeMemberMethod(MethodDefinition methodDefinition) {
     CodeMemberMethod codeMemberMethod;
     if(!methodDefinition.IsConstructor) {
         codeMemberMethod = new CodeMemberMethod() { Name = GetMemberMethodName(methodDefinition) };
     }
     else {
         codeMemberMethod = new CodeConstructor();
     }
     codeMemberMethod.Attributes = GetMemberAttributes(methodDefinition);
     FillCustomAttribute(codeMemberMethod.CustomAttributes, methodDefinition.CustomAttributes);
     FillGenericParameters(codeMemberMethod.TypeParameters, methodDefinition.GenericParameters);
     codeMemberMethod.ReturnType = new CodeTypeReference(GetTypeReferenceName(methodDefinition.ReturnType));
     bool isExtensionMethod = methodDefinition.IsExtensionMethod();
     List<ParameterDefinition> outParameters = new List<ParameterDefinition>();
     foreach(var parameter in methodDefinition.Parameters) {
         CodeParameterDeclarationExpression p = GetParameterExpression(parameter, isExtensionMethod && parameter == methodDefinition.Parameters.First());
         if(parameter.IsOut) {
             outParameters.Add(parameter);
         }
         codeMemberMethod.Parameters.Add(p);
     }
     if(methodDefinition.HasBody || !methodDefinition.IsAbstract) {
         if(methodDefinition.IsConstructor && methodDefinition.DeclaringType.IsValueType) {
             foreach(var fieldDefinition in methodDefinition.DeclaringType.Fields.Where(i => !i.IsStatic && IsEnableField(i))) {
                 CodeAssignStatement assign = new CodeAssignStatement(
                     new CodeArgumentReferenceExpression("this." + fieldDefinition.Name),
                     GetDefalutValue(fieldDefinition.FieldType));
                 codeMemberMethod.Statements.Add(assign);
             }
         }
         foreach(var outParameter in outParameters) {
             CodeAssignStatement assign = new CodeAssignStatement(
                 new CodeArgumentReferenceExpression(outParameter.Name),
                 GetDefalutValue(outParameter.ParameterType));
             codeMemberMethod.Statements.Add(assign);
         }
         if(methodDefinition.ReturnType.MetadataType != MetadataType.Void) {
             codeMemberMethod.Statements.Add(new CodeMethodReturnStatement(GetDefalutValue(methodDefinition.ReturnType)));
         }
     }
     if(methodDefinition.IsOpImplicit()) {
         string code = codeMemberMethod.GenCode();
         Utility.FixOpImplicit(ref code);
         return new CodeSnippetTypeMember(code);
     }
     else if(methodDefinition.IsOpExplicit()) {
         string code = codeMemberMethod.GenCode();
         Utility.FixOpExplicit(ref code);
         return new CodeSnippetTypeMember(code);
     }
     return codeMemberMethod;
 }