public static bool NamingConventionCheckFunc(EnumNamingConventionType enumNamingConventionType, string nameStr, ref string exceptionMsg, int maxLength = 20) { if (string.IsNullOrEmpty(nameStr)) { exceptionMsg = '名称为空'; return false; } if (nameStr.Length > maxLength) { exceptionMsg = '名称长度超过' + maxLength + '个字节'; return false; } if (nameStr.Contains(' ')) { exceptionMsg = '名称中含有无效字符'; return false; } List invalidChars = new List() { '?', '*', '<', '>', '|', '/', '"', ':', '' }; foreach (char c in nameStr) { if (!IsChinese(c) && !char.IsLetterOrDigit(c) && !invalidChars.Contains(c)) { exceptionMsg = '名称中含有无效字符'; return false; } } if (enumNamingConventionType == EnumNamingConventionType.ClassName || enumNamingConventionType == EnumNamingConventionType.FieldName) { if (!char.IsLetter(nameStr[0]) && nameStr[0] != '') { exceptionMsg = '命名不规范,名称首字母只能为大小写字母、下划线'; return false; } if (new string[] { 'abstract', 'as', 'base', 'bool', 'break', 'byte', 'case', 'catch', 'char', 'checked', 'class', 'const', 'continue', 'decimal', 'default', 'delegate', 'do', 'double', 'else', 'enum', 'event', 'explicit', 'extern', 'false', 'finally', 'fixed', 'float', 'for', 'foreach', 'goto', 'if', 'implicit', 'in', 'int', 'interface', 'internal', 'is', 'lock', 'long', 'namespace', 'new', 'null', 'object', 'operator', 'out', 'override', 'params', 'private', 'protected', 'public', 'readonly', 'ref', 'return', 'sbyte', 'sealed', 'short', 'sizeof', 'stackalloc', 'static', 'string', 'struct', 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'uint', 'ulong', 'unchecked', 'unsafe', 'ushort', 'using', 'virtual', 'void', 'volatile', 'while' }.Contains(nameStr)) { exceptionMsg = '命名不规范,名称中含有关键字!'; return false; } } return true; }

C# 命名规范校验函数 - 确保代码质量和可读性

原文地址: https://www.cveoy.top/t/topic/lK3N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录