Exemplo n.º 1
0
        MethodBody[] GetMethodBodies(IMemberReference member)
        {
            if (member is MethodReference)
            {
                return(new[] { (((MethodReference)member).Resolve()).Body });
            }
            if (member is PropertyReference)
            {
                PropertyDefinition prop = ((PropertyReference)member).Resolve();
                return(new[] {
                    prop.GetMethod != null ? prop.GetMethod.Body : null,
                    prop.SetMethod != null ? prop.SetMethod.Body : null,
                });
            }
            if (member is FieldReference)
            {
                return new MethodBody[] {}
            }
            ;
            if (member is EventReference)
            {
                EventDefinition ev = ((EventReference)member).Resolve();

                return(new[] {
                    ev.AddMethod != null ? ev.AddMethod.Body : null,
                    ev.InvokeMethod != null ? ev.InvokeMethod.Body : null,
                    ev.RemoveMethod != null ? ev.RemoveMethod.Body : null,
                });
            }
            throw new NotSupportedException("Unsupported member type: " + member.GetType().FullName);
        }

        void FillExceptions(MethodBody body, Dictionary <string, ExceptionSources> exceptions)
        {
            for (int i = 0; i < body.Instructions.Count; ++i)
            {
                Instruction instruction = body.Instructions [i];
                switch (instruction.OpCode.Code)
                {
                case Code.Call:
                case Code.Callvirt: {
                    if ((locations & ExceptionLocations.Assembly) == 0 &&
                        (locations & ExceptionLocations.DependentAssemblies) == 0)
                    {
                        break;
                    }
                    IMemberReference memberRef = ((IMemberReference)instruction.Operand);
                    if (((locations & ExceptionLocations.Assembly) != 0 &&
                         body.Method.DeclaringType.Scope.Name == memberRef.DeclaringType.Scope.Name) ||
                        ((locations & ExceptionLocations.DependentAssemblies) != 0 &&
                         body.Method.DeclaringType.Scope.Name != memberRef.DeclaringType.Scope.Name))
                    {
                        IEnumerable <ExceptionSources> memberExceptions = this [memberRef];
                        AddExceptions(body, instruction,
                                      memberExceptions.Select(es => es.Exception),
                                      memberExceptions.SelectMany(es => es.Sources),
                                      exceptions);
                    }
                    break;
                }

                case Code.Newobj: {
                    MethodReference ctor = (MethodReference)instruction.Operand;
                    if (IsExceptionConstructor(ctor))
                    {
                        AddExceptions(body, instruction,
                                      new TypeReference[] { ctor.DeclaringType },
                                      new IMemberReference[] { body.Method },
                                      exceptions);
                    }
                    break;
                }
                }
            }
        }

        void AddExceptions(MethodBody body, Instruction instruction, IEnumerable <TypeReference> add, IEnumerable <IMemberReference> sources,
                           Dictionary <string, ExceptionSources> exceptions)
        {
            var handlers = body.ExceptionHandlers.Cast <ExceptionHandler> ()
                           .Where(eh => instruction.Offset >= eh.TryStart.Offset &&
                                  instruction.Offset <= eh.TryEnd.Offset);

            foreach (var ex in add)
            {
                if (!handlers.Any(h => IsExceptionCaught(ex, h.CatchType)))
                {
                    ExceptionSources s;
                    string           eName = xdoc.GetDeclaration(ex);
                    if (!exceptions.TryGetValue(eName, out s))
                    {
                        s = new ExceptionSources(ex);
                        exceptions.Add(eName, s);
                    }
                    s.SourcesList.AddRange(sources);
                }
            }
        }
Exemplo n.º 2
0
		void AddExceptions (MethodBody body, Instruction instruction, IEnumerable<TypeReference> add, IEnumerable<MemberReference> sources,
				Dictionary<string, ExceptionSources> exceptions)
		{
			var handlers = body.ExceptionHandlers.Cast<ExceptionHandler> ()
				.Where (eh => instruction.Offset >= eh.TryStart.Offset && 
						instruction.Offset <= eh.TryEnd.Offset);
			foreach (var ex in add) {
				if (!handlers.Any (h => IsExceptionCaught (ex, h.CatchType))) {
					ExceptionSources s;
					string eName = xdoc.GetDeclaration (ex);
					if (!exceptions.TryGetValue (eName, out s)) {
						s = new ExceptionSources (ex);
						exceptions.Add (eName, s);
					}
					s.SourcesList.AddRange (sources);
				}
			}
		}