Example #1
0
        protected List <TypeElement> CreateTypeElements(NamespaceContext context, List <DefinedType> types)
        {
            List <TypeElement> result = new List <TypeElement>();

            foreach (DefinedType dt in types)
            {
                TypeElement te = new TypeElement();
                te.FromSource  = context.FileName;
                te.EndPos      = dt.EndPos;
                te.Name        = dt.Name;
                te.Namespace   = context.Namespace;
                te.BaseType    = ResolveBaseType(context, dt.BaseType);
                te.Interfaces  = ResolveFullNames(context, dt.Interfaces);
                te.Methods     = ProcessMethods(context, dt.Methods);
                te.Fields      = ProcessFields(context, dt.Fields);
                te.IsClass     = dt.IsClass;
                te.IsInterface = dt.IsInterface;
                te.IsAbstract  = dt.IsAbstract;
                te.IsSealed    = dt.IsSealed;
                te.IsPublic    = dt.IsPublic;
                result.Add(te);

                // set method signatures
                foreach (MethodElement method in te.Methods)
                {
                    method.Signature = MethodElement.GenerateSignature(te.FullName, method);
                }

                ResolveReferencedTypes(context, dt.ReferencedTypes);
                CreateTypeElements(context, dt.DefinedTypes);
            }
            return(result);
        }
Example #2
0
        public override void VisitMethodDefinition(MethodDefinition method)
        {
            // If we only extract the unresolvedtypes then we most likely are only interested
            // in methods which can be overriden. So skip the rest.
            if (ExtractUnresolvedOnly && !method.IsVirtual)
            {
                return;
            }

            // Create a new method element
            MethodElement me = new MethodElement();

            me.Name          = method.Name;
            me.ReturnType    = method.ReturnType.ReturnType.FullName;
            me.IsAbstract    = method.IsAbstract;
            me.IsConstructor = method.IsConstructor;
            me.IsStatic      = method.IsStatic;
            me.IsVirtual     = method.IsVirtual;

            MethodAttributes memberAccess = method.Attributes & MethodAttributes.MemberAccessMask;

            me.IsPrivate = memberAccess == MethodAttributes.Private;
            me.IsPublic  = memberAccess == MethodAttributes.Public;

            // Add the parameters
            foreach (ParameterDefinition param in method.Parameters)
            {
                ParameterElement pe = new ParameterElement();

                pe.Name    = param.Name;
                pe.Ordinal = (short)(param.Sequence);
                pe.Type    = param.ParameterType.FullName;

                if ((param.Attributes & Mono.Cecil.ParameterAttributes.Out) != Mono.Cecil.ParameterAttributes.Out)
                {
                    pe.ParameterOption |= ParameterOptions.In;
                }
                else
                {
                    pe.ParameterOption &= ~ParameterOptions.In;
                }

                if ((param.Attributes & Mono.Cecil.ParameterAttributes.Out) == Mono.Cecil.ParameterAttributes.Out)
                {
                    pe.ParameterOption |= ParameterOptions.Out;
                }

                if ((param.Attributes & Mono.Cecil.ParameterAttributes.Optional) == Mono.Cecil.ParameterAttributes.Optional)
                {
                    pe.ParameterOption |= ParameterOptions.Optional;
                }

                // Remark; we do not harvest custom attributes here.

                me.Parameters.Add(pe);
            }

            // Add the method body
            if (ProcessMethodBody && method.HasBody)
            {
                me.Body = new Entities.LanguageModel.MethodBody();

                List <string> callList = new List <string>();

                foreach (Instruction instr in method.Body.Instructions)
                {
                    if (instr.OpCode.Value == OpCodes.Call.Value ||
                        instr.OpCode.Value == OpCodes.Calli.Value ||
                        instr.OpCode.Value == OpCodes.Callvirt.Value
                        )
                    {
                        CallElement ce = new CallElement();

                        // instr.Operand can be a MethodReference or a CallSite
                        ce.MethodReference = instr.Operand.ToString();

                        if (!callList.Contains(ce.MethodReference))
                        {
                            me.Body.Calls.Add(ce);
                            callList.Add(ce.MethodReference);
                        }
                    }
                }
            }

            // Custom attributes
            me.Attributes.AddRange(ExtractCustomAttributes(method.CustomAttributes));

            // Set the signature
            string declaringTypeName = method.DeclaringType.FullName;

            me.Signature = MethodElement.GenerateSignature(declaringTypeName, me);

            // Add to the type
            _currentType.Methods.Add(me);
        }