static public BindingFlags ToBindingFlags(this AccessModifiers accessModifiers) { BindingFlags flags = 0; if (accessModifiers.HasFlag(AccessModifiers.Private)) { flags |= BindingFlags.NonPublic; } if (accessModifiers.HasFlag(AccessModifiers.Public)) { flags |= BindingFlags.Public; } return(flags); }
public static void Check(IMethodInfo method, string assemblyName, bool allowProtected = false) { AccessModifiers am = method.AccessModifiers; if (am.HasFlag(AccessModifiers.Internal)) { bool grantedByAttr = method .DeclaringType .DeclaringAssembly ?.IsFriend(assemblyName) == true; if (grantedByAttr) { return; } if (!am.HasFlag(AccessModifiers.Protected) /*protected-internal*/) { throw new MemberAccessException(string.Format(Resources.Culture, Resources.IVT_REQUIRED, method.Name, assemblyName)); } } if (am.HasFlag(AccessModifiers.Protected)) { if (allowProtected) { return; } throw new MemberAccessException(string.Format(Resources.Culture, Resources.METHOD_NOT_VISIBLE, method.Name)); } // // "Private", "Explicit" mellett mas nem szerepelhet -> nem kell HasFlag() // if (am == AccessModifiers.Explicit) { return; // meg ha cast-olni is kell hozza de lathato } if (am == AccessModifiers.Private) { throw new MemberAccessException(string.Format(Resources.Culture, Resources.METHOD_NOT_VISIBLE, method.Name)); } Debug.Assert(am == AccessModifiers.Public, $"Unknown AccessModifier: {am}"); }
private static List <string> ModifiersToStrings(AccessModifiers modifiers) { var s = new List <string>(); // TODO fairly bad, no semantics and not forced to handle all modifiers if (modifiers.HasFlag(AccessModifiers.Public)) { s.Add("public"); } if (modifiers.HasFlag(AccessModifiers.Private)) { s.Add("private"); } if (modifiers.HasFlag(AccessModifiers.Protected)) { s.Add("protected"); } if (modifiers.HasFlag(AccessModifiers.Static)) { s.Add("static"); } if (modifiers.HasFlag(AccessModifiers.Abstract)) { s.Add("abstract"); } if (modifiers.HasFlag(AccessModifiers.Partial)) { s.Add("partial"); } if (modifiers.HasFlag(AccessModifiers.Readonly)) { s.Add("readonly"); } return(s); }