//static public string GenerateCode(string prefix = "", byte subscriptionID = 0, int length = 6) //{ // var service = new CoderService(36); // string codePrefix = String.IsNullOrEmpty(prefix) ? "" : (prefix.TrimEnd('-') + "-"); // string codeSubscr = subscriptionID <= 0 ? "" : (subscriptionID.ToString() + "-"); // if (String.IsNullOrEmpty(codePrefix) && !String.IsNullOrEmpty(codeSubscr)) // codeSubscr = "S" + codeSubscr; // string codeDate = service.Encode(CommonService.Now.DateTime); // string codeRandom = service.Generate(length <= 0 ? 6 : length); // return String.Format("{0}{1}{2}-{3}", codePrefix, codeSubscr, codeDate, codeRandom); //} public static string GenerateEntityCode(string prefix = "", int length = 8) { var service = new CoderService(36); string codePrefix = String.IsNullOrEmpty(prefix) ? "" : (prefix.TrimEnd('-') + "-"); string codeDate = service.Encode(CommonService.Now.DateTime); string codeRandom = service.Generate(length <= 0 ? 8 : length); return String.Format("{0}{1}-{2}", codePrefix, codeDate, codeRandom); }
public static string GenerateCode(int codeLength, int baseLength = MaxBaseLength, bool capitalize = false) { var coder = new CoderService(baseLength); var code = coder.Generate(codeLength).ToLower(); if (capitalize && code.Length > 1) { var rand = CommonService.Randomizer; int capitalCount = rand.Next(1, code.Length); char[] chars = code.ToCharArray(); for (int i = 0; i < capitalCount; i++) { int index = rand.Next(0, chars.Length); chars[index] = Char.ToUpper(chars[index]); } code = new String(chars); } return code; }