사전에 열거하고 그 형식 다음에 사전의 각 이름 (값)을 변환해야합니다. 나는 이런 식으로 뭔가해야 할 그 후사전에 열거하고 각 이름을 형식화하십시오.
public static Dictionary<int, string> EnumToDictionary<TK>(Func<TK, string > func)
{
if (typeof(TK).BaseType != typeof(Enum))
throw new InvalidCastException();
return Enum.GetValues(
typeof(TK))
.Cast<Int32>()
.ToDictionary(
currentItem => currentItem => Enum.GetName(typeof(TK), currentItem))
/*. func for each name */;
}
public enum Types {
type1 = 0,
type2 = 1,
type3 = 2
}
public string FormatName(Types t) {
switch (t) {
case Types.type1:
return "mytype1";
case Types.type2:
return "mytype2";
case Types.type3:
return "mytype3";
default:
return string.Empty;
}
}
:
var resultedAndFormatedDictionary =
EnumToDictionary<Types>(/*delegate FormatName() for each element of dictionary() */);
을 어떻게 정의하고 사전의 각 값에 대한 몇 가지 작업을 수행 위임 (Func<object, string > func
)를 구현합니까?
UPDATE : Correspoding 결과는 int로서 열거 값이 키와 그것에 대한 어떤 형식의 이름을 열거에서 사전을 만들기 위해 달성하고자하는 질문에서 추측
var a = EnumToDictionary<Types>(FormatName);
//a[0] == "mytype1"
//a[1] == "mytype2"
//a[2] == "mytype3"
당신이 작은이 코드를 포맷 할 수 있습니다? –
질문 할 수도 있습니까? 귀하의 전체 게시물은 지금까지의 진술 일뿐입니다. 뭐가 문제 야? – ChrisWue
그리고 몇 가지 샘플 입력 데이터와 해당 결과를 제공하십시오 –