指定字符串作为可能的参数-枚举



对于我正在编写的工具,我需要一个"随机文本"生成器。我想让用户能够从这样的预制字符串中进行选择:

const string baseCollection = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string numbers = "0123456789";
const string specialChars = "°^!"§$%&/{([)]=}?\`´*+~'#,;.:-_><|";
const string germanAddition = "ÄÖÜäöü";
const string frenchAddition = "éàèùâêîôûäëïü眫»";
const string russianAddition = " бвгджзклмнпрстфхцчшщйаэыуояеёюиьъ";

应该使用哪个术语来运行此方法。

public string RanText(int length, ???)
{
string charCollectionString = "";
foreach(string str in charCollectionStrings) {
charCollectionString += str;
}
//stuff
return finalString;
}

我曾想过使用枚举,但这些枚举不允许使用字符串。创建一系列可能的参数的最干净的方法是什么?

您是否考虑过使用Dictionary<>?例如:

public enum CType
{
Base,
Numbers,
Special,
German,
French,
Russian
}
public readonly Dictionary<CType, string> Collections = new Dictionary<CType, string>
{
{ CType.Base, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" },
{ CType.Numbers, "0123456789" },
{ CType.Special, "°^!"§$%&/{([)]=}?\`´*+~'#,;.:-_><|" },
{ CType.German, "ÄÖÜäöü" },
{ CType.French, "éàèùâêîôûäëïüçœ" },
{ CType.Russian, "бвгджзклмнпрстфхцчшщйаэыуояеёюиьъ" }
};
public string RanText(int length, CType[] parameters)
{
string charCollectionString = "";
foreach (CType param in parameters)
{
charCollectionString += Collections[param];
}
}

然后:

RanText(1, new[] { CType.Base, CType.Numbers, CType.Russian });

最新更新