Example #1
0
        internal static bool IsOption(this ICustomAttributeProvider attributeProvider, ArgumentMode argumentMode)
        {
            // If developer defined the mode with an attribute, use that,
            // otherwise use the defined ArgumentMode

            return(argumentMode switch
            {
                ArgumentMode.Operand => attributeProvider.HasAttribute <OptionAttribute>(),
                ArgumentMode.Option => !attributeProvider.HasAttribute <OperandAttribute>(),
                _ => throw new ArgumentOutOfRangeException(nameof(argumentMode), argumentMode, null)
            });
Example #2
0
        public static bool HasColumnAccess(this ICustomAttributeProvider provider, Access access)
        {
            PropertyInfo property = provider as PropertyInfo;

            if (property != null)
            {
                Type t = property.ReflectedType;
                ColumnAccessAttribute col;
                if (t.HasAttribute(out col, attr => attr.ColumnName.Equals(property.SqlColumnName())))
                {
                    return(col.Access == access);
                }
            }

            return(!provider.HasAttribute <ColumnAccessAttribute>() || provider.HasAttribute <ColumnAccessAttribute>(attr => attr.Access == access));
        }
Example #3
0
 public static ReferenceType<Boolean3> Draw3(Rect position, object eventsObj, GUIContent content, ICustomAttributeProvider info = null)
 {
     Boolean3 boolPair = (Boolean3)eventsObj;
     var boolArray = new bool[] { boolPair.x, boolPair.y, boolPair.z };
     Builder = new StringBuilder();
     char flags = (char)EditorNameFlags.Default;
     if (info != null && info.HasAttribute(typeof(CustomNamesAttribute)))
     {
         CustomNamesAttribute attribute = info.GetCustomAttributes<CustomNamesAttribute>()[0];
         flags = (char)attribute.CustomFlags;
         Builder.Append(flags);
         if (attribute.UseVariableNameAsTitle)
         {
             Builder.Append(Seperator);
             Builder.Append(content.text);
         }
         Builder.Append(attribute.CombinedName);
     }
     else
     {
         Builder.Append(flags);
         Builder.Append(Seperator);
         Builder.Append(content.text);
     }
     content.text = Builder.ToString();
     DrawMultiBoolean(ref boolArray, position, content);
     boolPair.x = boolArray[0];
     boolPair.y = boolArray[1];
     boolPair.z = boolArray[2];
     return boolPair;
 }
        /// <summary>
        /// Registers static interceptors defined by attributes on the class for all candidate
        /// methods on the class, execept those decorated with a <see cref="DoNotInterceptAttribute"/>.
        /// </summary>
        /// <param name="type">The type whose activation plan is being manipulated.</param>
        /// <param name="plan">The activation plan that is being manipulated.</param>
        /// <param name="candidates">The candidate methods to intercept.</param>
        protected virtual void RegisterClassInterceptors(Type type, IPlan plan, IEnumerable <MethodInfo> candidates)
        {
            InterceptAttribute[] attributes = type.GetAllAttributes <InterceptAttribute>();

            if (attributes.Length == 0)
            {
                return;
            }

            foreach (MethodInfo method in candidates)
            {
                PropertyInfo             property = method.GetPropertyFromMethod(type);
                ICustomAttributeProvider provider = (ICustomAttributeProvider)property ?? method;

                if (!provider.HasAttribute <DoNotInterceptAttribute>())
                {
                    RegisterMethodInterceptors(type, method, attributes);
                }
            }

            // Indicate that instances of the type should be proxied.
            if (!plan.Has <ProxyDirective>())
            {
                plan.Add(new ProxyDirective());
            }
        }
        private void CheckSuppressUnmanagedCodeSecurity(ICustomAttributeProvider type, bool required)
        {
            string msg = null;

            if (type.HasAttribute("System.Security", "SuppressUnmanagedCodeSecurityAttribute"))
            {
                if (!required)
                {
                    msg = "Remove [SuppressUnmanagedCodeSecurity] attribute on the type declaration.";
                }
            }
            else
            {
                // no [SuppressUnmanagedCodeSecurity] attribute
                if (required)
                {
                    msg = "Add missing [SuppressUnmanagedCodeSecurity] attribute on the type declaration.";
                }
            }

            if (msg != null)
            {
                Runner.Report(type, Severity.Critical, Confidence.Total, msg);
            }
        }
Example #6
0
        /// <summary>
        /// Checks if the provided name matches the next name on the stream. Returns true if the entire member should be skipped.
        /// </summary>
        /// <param name="reader">The reader to read the name from.</param>
        /// <param name="member">The member info.</param>
        /// <param name="name">The expected name to find on the stream.</param>
        /// <returns>Returns true if the property must be ignored, because it was found to be an optional field. Returns false if the name read from stream is read succesfully and matches the provided name..</returns>
        public static bool SkipMember(this BinaryReader reader, ICustomAttributeProvider member, string name)
        {
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            // If a name was passed, a name is expected on the stream. Read the name and validate it.
            string nameOnStream = reader.ReadNullTerminatedString();

            if (string.Compare(name, nameOnStream, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(false);                // Matches, so don't skip.
            }

            // If memberInfo is type, then a controller name was expected.
            if (member is Type)
            {
                throw new SilentHunterParserException($"Expected controller name '{name}', but read '{nameOnStream}' from the stream.");
            }

            if (!member.HasAttribute <OptionalAttribute>())
            {
                throw new SilentHunterParserException($"The reflected name '{name}' does not match the name '{nameOnStream}' read from the stream.");
            }

            // Property must be skipped.
            return(true);
        }
Example #7
0
        private static bool IsOption(ICustomAttributeProvider attributeProvider, ArgumentMode argumentMode)
        {
            // developer can force the argument mode with an attribute

            switch (argumentMode)
            {
            case ArgumentMode.Parameter:
                return(attributeProvider.HasAttribute <OptionAttribute>());

            case ArgumentMode.Option:
                return(!attributeProvider.HasAttribute <ArgumentAttribute>());

            default:
                throw new ArgumentOutOfRangeException(nameof(argumentMode), argumentMode, null);
            }
        }
Example #8
0
        internal static bool IsOption(this ICustomAttributeProvider attributeProvider, ArgumentMode argumentMode)
        {
            // If developer defined the mode with an attribute, use that,
            // otherwise use the defined ArgumentMode

            switch (argumentMode)
            {
            case ArgumentMode.Operand:
                return(attributeProvider.HasAttribute <OptionAttribute>());

            case ArgumentMode.Option:
                return(!attributeProvider.HasAttribute <OperandAttribute>() &&
                       !attributeProvider.HasAttribute <ArgumentAttribute>());

            default:
                throw new ArgumentOutOfRangeException(nameof(argumentMode), argumentMode, null);
            }
        }
        public static CustomAttribute AddAttribute(this ICustomAttributeProvider member, TypeReference attribute, bool allowRepeating = false)
        {
            if (!allowRepeating && member.HasAttribute(x => x.AttributeType.FullName == attribute.FullName))
            {
                return(null);
            }
            var attr = new CustomAttribute(attribute.Module.ImportReference(attribute.Resolve().Methods.FirstOrDefault(x => x.IsConstructor)));

            member.CustomAttributes.Add(attr);
            return(attr);
        }
Example #10
0
        public static bool TryGetAttributes <T>(this ICustomAttributeProvider provider, bool inherit,
                                                out IEnumerable <T> attributes) where T : Attribute
        {
            if (!provider.HasAttribute <T>())
            {
                attributes = Enumerable.Empty <T>();
                return(false);
            }

            attributes = provider.GetAttributes <T>(inherit);
            return(true);
        }
Example #11
0
        public static bool TryGetAttribute <T>(this ICustomAttributeProvider provider, bool inherit, out T attribute)
            where T : Attribute
        {
            if (!provider.HasAttribute <T>())
            {
                attribute = default;
                return(false);
            }

            foreach (var attr in provider.GetAttributes <T>(inherit))
            {
                attribute = attr;
                return(true);
            }

            attribute = default;
            return(false);
        }
Example #12
0
    static bool HasFriendAccessAllowedAttribute(MemberReference member, bool declaringType)
    {
        if (member == null)
        {
            return(false);
        }

        ICustomAttributeProvider cap = (member as ICustomAttributeProvider);

        if (cap.HasCustomAttributes)
        {
            if (cap.HasAttribute(FriendAccessAllowedAttribute))
            {
                return(true);
            }
        }
        return(declaringType ? HasFriendAccessAllowedAttribute(member.DeclaringType, declaringType) : false);
    }
Example #13
0
        public static string GetDisplayName(this ICustomAttributeProvider provider)
        {
            if (provider.HasAttribute <DisplayNameAttribute>())
            {
                return(provider.GetAttribute <DisplayNameAttribute>().DisplayName);
            }
            else
            {
                string     name = provider.ToString().Prettify();
                MemberInfo mi   = provider as MemberInfo;
                if (mi != null)
                {
                    name = mi.Name.Prettify();
                }

                string[] arr = name.Split(' ');
                if ((arr.Length > 1) && (arr[arr.Length - 1].ToLower() == "id"))
                {
                    arr[arr.Length - 1] = "";
                }
                return(arr.Delimit(" ", "").TrimEnd());
            }
        }
Example #14
0
        /// <summary>
        /// Determines whether the <paramref name="provider"/> element has an associated <see href="Attribute"/>
        /// of all of the types given in <paramref name="attributeTypes"/>.
        /// </summary>
        /// <returns>True if the source element has all of the specified attribute types, false otherwise.</returns>
        public static bool HasAllAttributes(this ICustomAttributeProvider provider, params Type[] attributeTypes)
        {
            bool hasTypes = attributeTypes != null && attributeTypes.Length > 0;

            return(!hasTypes || attributeTypes.All(at => provider.HasAttribute(at)));
        }
Example #15
0
 /// <summary>
 /// Determines whether the <paramref name="provider"/> element has an associated <see href="Attribute"/>
 /// of type <typeparamref name="T"/>.
 /// </summary>
 /// <returns>True if the source element has the associated attribute, false otherwise.</returns>
 public static bool HasAttribute <T>(this ICustomAttributeProvider provider) where T : Attribute
 {
     return(provider.HasAttribute(typeof(T)));
 }
        public static bool HasAttribute <T>(this ICustomAttributeProvider member)
#endif
            where T : Attribute
        {
            return(member.HasAttribute <T>(false));
        }
Example #17
0
		/// <summary>
		/// Translates the type.
		/// </summary>
		/// <param name="fieldInfo">The field info.</param>
		/// <param name="fieldType">Type of the field.</param>
		/// <returns></returns>
		public static string ToCppTypename(ICustomAttributeProvider fieldInfo, Type fieldType)
		{
			string translatedType;

			if (fieldType == typeof(string)) {
				return fieldInfo.HasAttribute<MarshalAsAttribute>() &&
					   fieldInfo.GetAttribute<MarshalAsAttribute>(false).Value == UnmanagedType.LPStr
						? "_string"
						: "_wstring";
			}

			if (TranslatedTypes.TryGetValue(fieldType, out translatedType))
				return translatedType;

			if (fieldType.IsByRef) {
				string pointedTypename = fieldType.FullName.Substring(0, fieldType.FullName.Length - 1);
				Type pointedType = fieldType.Assembly.GetType(pointedTypename);

				return ToCppTypename(fieldInfo, pointedType) + "*";
			}

			if (fieldType.IsArray) {
				return ToCppTypename(fieldInfo, fieldType.GetElementType()) + "*";
			}

			if (fieldType.IsPointer) {
				string pointedTypename = fieldType.FullName.Substring(0, fieldType.FullName.Length - 1);
				Type pointedType = fieldType.Assembly.GetType(pointedTypename);

				return ToCppTypename(fieldInfo, pointedType) + "*";
			}

			if (fieldType.IsEnum)
				return GetCppEnumName(fieldType);

			if (fieldType.HasICppInterface())
				return ToCppTypename(fieldInfo, typeof(Handle));

			return GetCppTypename(fieldType);
		}
		private void CheckSuppressUnmanagedCodeSecurity (ICustomAttributeProvider type, bool required)
		{
			string msg = null;
			if (type.HasAttribute ("System.Security", "SuppressUnmanagedCodeSecurityAttribute")) {
				if (!required)
					 msg = "Remove [SuppressUnmanagedCodeSecurity] attribute on the type declaration.";
			} else {
				// no [SuppressUnmanagedCodeSecurity] attribute
				if (required)
					msg = "Add missing [SuppressUnmanagedCodeSecurity] attribute on the type declaration.";
			}

			if (msg != null)
				Runner.Report (type, Severity.Critical, Confidence.Total, msg);
		}
Example #19
0
 public static bool IsAttributePresent <T>(ICustomAttributeProvider element) where T : Attribute
 {
     return(element.HasAttribute <T>());
 }
Example #20
0
 /// <summary>
 /// Determines if the specified <paramref name="attributeProvider"/> has an attribute of <typeparamref name="T"/> in its full implementation hierarchy.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type"/> of the attribute to locate.</typeparam>
 /// <param name="attributeProvider">The <see cref="ICustomAttributeProvider"/> instance to check.</param>
 /// <returns><c>True</c> if the <paramref name="attributeProvider"/> has an attribute of <typeparamref name="T"/>; otherwisew <c>False</c>.</returns>
 public static bool HasAttribute <T>(this ICustomAttributeProvider attributeProvider)
     where T : Attribute
 {
     return(attributeProvider.HasAttribute <T>(true));
 }
Example #21
0
        public static bool HasAttribute <TAttribute>(this ICustomAttributeProvider self, bool inherit = false)
        {
            var attributeType = typeof(TAttribute);

            return(self.HasAttribute(attributeType, inherit));
        }
Example #22
0
 public static ReferenceType<Integer2> Draw2(Rect position, object eventsObj, GUIContent content, ICustomAttributeProvider info = null)
 {
     Integer2 intPair = (Integer2)eventsObj;
     var intArray = new int[2]{ intPair.x, intPair.y };
     Builder = new StringBuilder();
     char flags = (char)EditorNameFlags.Default;
     if (info != null && info.HasAttribute(typeof(CustomNamesAttribute)))
     {
         CustomNamesAttribute attribute = info.GetCustomAttributes<CustomNamesAttribute>()[0];
         flags = (char)attribute.CustomFlags;
         Builder.Append(flags);
         if(attribute.UseVariableNameAsTitle)
         {
             Builder.Append(Seperator);
             Builder.Append(content.text);
         }
         Builder.Append(attribute.CombinedName);
     }
     else
     {
         Builder.Append(flags);
         Builder.Append(Seperator);
         Builder.Append(content.text);
     }
     content.text = Builder.ToString();
     DrawMultiInteger(ref intArray, position, content);
     intPair.x = intArray[0];
     intPair.y = intArray[1];
     return intPair;
 }
 public static bool IsCompilerGenerated(this ICustomAttributeProvider type)
 => type.HasAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute>();
 /// <summary>
 /// Determines if the given type had the attribute defined at least once.
 /// </summary>
 /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
 /// <param name="type">The type.</param>
 /// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute..</param>
 /// <returns>TAttribute.</returns>
 public static bool HasAttribute <TAttribute>(this ICustomAttributeProvider type, bool inherit = false)
     where TAttribute : Attribute
 {
     return(type.HasAttribute(typeof(TAttribute), inherit));
 }