Exemplo n.º 1
0
 /// <summary>
 /// Gets <see cref="Type"/> for the given <see cref="TypeDefinition"/>.
 /// </summary>
 /// <returns>The <see cref="Type"/> for the given <see cref="TypeDefinition"/></returns>
 public static Type GetType(this TypeDefinition value, AssemblyResolver assemblyResolver) =>
 assemblyResolver.GetType(value);
Exemplo n.º 2
0
        /// <summary>
        /// Checks whether the <see cref="TypeDefinition"/> has the given <see cref="Attribute"/>.
        /// If <paramref name="assemblyResolver"/> is null, the inheritence tree will not be traversed.
        /// </summary>
        /// <returns>Whether the <see cref="TypeDefinition"/> has the given <see cref="Attribute"/></returns>
        public static bool HasCustomAttribute <TAttribute>(this TypeDefinition value, AssemblyResolver assemblyResolver)
            where TAttribute : Attribute
        {
            if (assemblyResolver is null)
            {
                return(value.HasCustomAttribute <TAttribute>());
            }

            var resolvedType = value.GetType(assemblyResolver);

            if (resolvedType is null)
            {
                return(false);
            }

            return(resolvedType.GetCustomAttributes(typeof(TAttribute), true).Any());
        }
        /// <summary>
        /// Checks whether the <see cref="MethodDefinition"/> has the given <see cref="Attribute"/>.
        /// If <paramref name="assemblyResolver"/> is null, the inheritence tree will not be traversed.
        /// </summary>
        /// <returns>Whether the <see cref="MethodDefinition"/> has the given <see cref="Attribute"/></returns>
        public static bool HasCustomAttribute <TAttribute>(this MethodDefinition value, AssemblyResolver assemblyResolver)
            where TAttribute : Attribute
        {
            if (assemblyResolver is null)
            {
                return(value.HasCustomAttribute <TAttribute>());
            }

            var methodBase = value.GetMethodBase(assemblyResolver);

            if (methodBase is null)
            {
                return(false);
            }

            return(methodBase.GetCustomAttributes(typeof(TAttribute), true).Any());
        }
 /// <summary>
 /// Checks whether the given method or its declaring type have the <see cref="EnablePerformanceProfilerAttribute"/>
 /// or the <see cref="DisablePerformanceProfilerAttribute"/> attribute, searching the inheritence tree.
 /// </summary>
 /// <returns>Whether performance profiler should be enabled for the given method.</returns>
 public static bool ShouldEnableProfiler(this MethodDefinition value, AssemblyResolver assemblyResolver) =>
 value.ShouldEnable <EnablePerformanceProfilerAttribute, DisablePerformanceProfilerAttribute>(assemblyResolver);
 /// <summary>
 /// Checks whether the given method or its declaring type have the <see cref="EnableExceptionLoggingAttribute"/>
 /// or the <see cref="DisableExceptionLoggingAttribute"/> attribute, searching the inheritence tree.
 /// </summary>
 /// <returns>Whether exception logging should be enabled for the given method.</returns>
 public static bool ShouldEnableLogging(this MethodDefinition value, AssemblyResolver assemblyResolver) =>
 value.ShouldEnable <EnableExceptionLoggingAttribute, DisableExceptionLoggingAttribute>(assemblyResolver);
 /// <summary>
 /// Gets the types of the method's parameters.
 /// </summary>
 /// <returns>The types of the method's parameters.</returns>
 public static IEnumerable <Type> GetTypeParameters(this MethodDefinition value, AssemblyResolver assemblyResolver) =>
 value.Parameters.Select(p => assemblyResolver.GetType(p.ParameterType, true));
        private static bool ShouldEnable <TEnable, TDisable>(this MethodDefinition value, AssemblyResolver assemblyResolver)
            where TEnable : Attribute
            where TDisable : Attribute
        {
            var methodHasDisableAttribute = value.HasCustomAttribute <TDisable>(assemblyResolver);

            if (methodHasDisableAttribute)
            {
                return(false);
            }

            var parentHasEnableAttribute = value.DeclaringType.HasCustomAttribute <TEnable>(assemblyResolver);
            var methodHasEnableAttribute = value.HasCustomAttribute <TEnable>(assemblyResolver);

            return(parentHasEnableAttribute || methodHasEnableAttribute);
        }