Ejemplo n.º 1
0
		/// <summary>
		/// Extracts methods and add them to method list for type.
		/// </summary>
		/// <param name="type">A type where methods will be added</param>
		/// <param name="methods">A collection of methods</param>
		private void ReadMethods(Type type, Collection<MethodDefinition> methods)
		{
			foreach (MethodDefinition methodDefinition in methods)
			{
				var method = new Method
				{
					Name = FormatMethodName(methodDefinition),
					DeclaringType = type,
					IsConstructor = methodDefinition.IsConstructor,
					IsPublic = methodDefinition.IsPublic,
					IsPrivate = methodDefinition.IsPrivate,
					IsProtected = !methodDefinition.IsPublic && !methodDefinition.IsPrivate,
					IsStatic = methodDefinition.IsStatic,
					IsSealed = methodDefinition.IsFinal, // not sure if final is sealed
					IsAbstract = methodDefinition.IsAbstract,
					IsSetter = methodDefinition.IsSetter,
					IsGetter = methodDefinition.IsGetter,
					IsVirtual = methodDefinition.IsVirtual,
					Variables = methodDefinition.Body != null ? methodDefinition.Body.Variables.Count : 0
				};

				var returnType =
					(from n in type.Namespace.Module.Namespaces
					 from t in n.Types
					 where t.FullName == FormatTypeName(methodDefinition.ReturnType, true)
					 select t).SingleOrDefault();

				method.ReturnType = returnType; // if null so return type is outside of assembly

				if (methodDefinition.ReturnType.IsGenericInstance)
				{
					method.IsReturnTypeGenericInstance = true;
					method.GenericReturnTypes.UnionWith(ReadGenericArguments(type.Namespace.Module,
					                                                         (GenericInstanceType) methodDefinition.ReturnType));
				}

				// reading types from parameters
				foreach (var parameter in methodDefinition.Parameters)
				{
					var parameterType =
						(from n in type.Namespace.Module.Namespaces
						 from t in n.Types
						 where t.FullName == FormatTypeName(parameter.ParameterType, true)
						 select t).SingleOrDefault();

					if (parameterType != null)
					{
						var param = new MethodParameter
						{
							ParameterType = parameterType,
							IsIn = parameter.IsIn,
							IsOut = parameter.IsOut,
							IsOptional = parameter.IsOptional,
						};

						// generic parameters
						if (parameter.ParameterType.IsGenericInstance)
						{
							param.IsGenericInstance = true;
							param.GenericTypes = ReadGenericArguments(type.Namespace.Module,
							                                          (GenericInstanceType) parameter.ParameterType);
						}

						method.Parameters.Add(param);
					}
				}

				type.Methods.Add(method);
			}

			foreach (MethodDefinition methodDefinition in methods)
			{
				var method = (from m in type.Methods
				              where m.Name == FormatMethodName(methodDefinition)
				              select m).SingleOrDefault();

				if (methodDefinition.Body != null)
				{
					ReadInstructions(method, methodDefinition, methodDefinition.Body.Instructions);
				}
			}
		}
Ejemplo n.º 2
0
        /// <summary>
        /// Extracts methods and add them to method list for type.
        /// </summary>
        /// <param name="type">A type where methods will be added</param>
        /// <param name="methods">A collection of methods</param>
        private void ReadMethods(Type type, Collection <MethodDefinition> methods)
        {
            foreach (MethodDefinition methodDefinition in methods)
            {
                var method = new Method
                {
                    Name          = FormatMethodName(methodDefinition),
                    DeclaringType = type,
                    IsConstructor = methodDefinition.IsConstructor,
                    IsPublic      = methodDefinition.IsPublic,
                    IsPrivate     = methodDefinition.IsPrivate,
                    IsProtected   = !methodDefinition.IsPublic && !methodDefinition.IsPrivate,
                    IsStatic      = methodDefinition.IsStatic,
                    IsSealed      = methodDefinition.IsFinal,                // not sure if final is sealed
                    IsAbstract    = methodDefinition.IsAbstract,
                    IsSetter      = methodDefinition.IsSetter,
                    IsGetter      = methodDefinition.IsGetter,
                    IsVirtual     = methodDefinition.IsVirtual,
                    Variables     = methodDefinition.Body != null ? methodDefinition.Body.Variables.Count : 0
                };

                var returnType =
                    (from n in type.Namespace.Module.Namespaces
                     from t in n.Types
                     where t.FullName == FormatTypeName(methodDefinition.ReturnType, true)
                     select t).SingleOrDefault();

                method.ReturnType = returnType;                 // if null so return type is outside of assembly

                if (methodDefinition.ReturnType.IsGenericInstance)
                {
                    method.IsReturnTypeGenericInstance = true;
                    method.GenericReturnTypes.UnionWith(ReadGenericArguments(type.Namespace.Module,
                                                                             (GenericInstanceType)methodDefinition.ReturnType));
                }

                // reading types from parameters
                foreach (var parameter in methodDefinition.Parameters)
                {
                    var parameterType =
                        (from n in type.Namespace.Module.Namespaces
                         from t in n.Types
                         where t.FullName == FormatTypeName(parameter.ParameterType, true)
                         select t).SingleOrDefault();

                    if (parameterType != null)
                    {
                        var param = new MethodParameter
                        {
                            ParameterType = parameterType,
                            IsIn          = parameter.IsIn,
                            IsOut         = parameter.IsOut,
                            IsOptional    = parameter.IsOptional,
                        };

                        // generic parameters
                        if (parameter.ParameterType.IsGenericInstance)
                        {
                            param.IsGenericInstance = true;
                            param.GenericTypes      = ReadGenericArguments(type.Namespace.Module,
                                                                           (GenericInstanceType)parameter.ParameterType);
                        }

                        method.Parameters.Add(param);
                    }
                }

                type.Methods.Add(method);
            }

            foreach (MethodDefinition methodDefinition in methods)
            {
                var method = (from m in type.Methods
                              where m.Name == FormatMethodName(methodDefinition)
                              select m).SingleOrDefault();

                if (methodDefinition.Body != null)
                {
                    ReadInstructions(method, methodDefinition, methodDefinition.Body.Instructions);
                }
            }
        }