/// <summary> /// Ensures that the proxy assembly contains no private, protected or internal methods. Those methods are dangerous in this context, since a call to a non-public method from a proxy one will compile (since both methods are in the same class), but then result in a runtime exception (since the method will be in a different class). /// </summary> /// <param name="proxy">The proxy assembly.</param> /// <exception cref="ProxyMistakeException">The assembly contains non-public methods.</exception> private void CheckForNonPublicMethods(IAssembly proxy) { var privateMethods = proxy .FindTypes(MemberType.Static, typeof(ProxyOfAttribute)) .SelectMany(t => t.FindMethods(MemberType.Static).Select(m => new { FullName = t.FullName + ":" + m.Name, IsPublic = m.IsPublic })) .Where(m => !m.IsPublic) .Select(m => m.FullName) .ToList(); switch (privateMethods.Count) { case 0: break; case 1: throw new ProxyMistakeException(string.Format("The proxy assembly is not expected to contain private methods, since after the merge, they could be called from a tampered class. The following private method was found: {0}.", privateMethods.Single())); default: throw new ProxyMistakeException(string.Format("The proxy assembly is not expected to contain private methods, since after the merge, they could be called from a tampered class. The following private methods were found: {0}.", string.Join("; ", privateMethods))); } }
/// <summary> /// Ensures that the proxy assembly contains no instance types. Instance types don't make much sense in a context of a proxy, and so indicate with high probability a typo. /// </summary> /// <param name="proxy">The proxy assembly.</param> /// <exception cref="ProxyMistakeException">The assembly contains instance types.</exception> private void CheckForInstanceTypes(IAssembly proxy) { Contract.Requires(proxy != null); var instanceTypes = proxy.FindTypes(MemberType.Instance).Select(t => t.FullName).Except(new[] { "<Module>" }).ToList(); switch (instanceTypes.Count) { case 0: break; case 1: throw new ProxyMistakeException(string.Format("The proxy assembly is not expected to contain instance types. The following instance type was found: {0}.", instanceTypes.Single())); default: throw new ProxyMistakeException(string.Format("The proxy assembly is not expected to contain instance types. The following instance types were found: {0}.", string.Join("; ", instanceTypes))); } }