English 中文(简体)
能否将埃南重新分类?
原标题:Can an Enum be serialized completely?

I ve got a .NET application conducted on WCF. 在这项申请中,我将各种“Types”定义为“CourseType”、“PresentationType”、“HierarchyType”等。 这些是自动与数据库合并的,因此,我可以撰写像:

public enum CourseType {
  Online = 1, 
  Classroom = 2
}

......

if(course.Type == CourseType.Online) {
  // do stuff on the server
}

我很想知道,是否有任何人知道将entire<>/em>系列起来的冰 way。 因此,我可以在 Java写类似声明。

请注意,I mnot 要求仅就价值进行序列化。 最后,我要谈谈一些 Java语,如:

CourseType = {
   online  : 1,
   classroom : 2
};

我知道,我可以通过思考来做到这一点,但我希望能够找到某种解决办法。...

最佳回答

在我看来,如果信标相对静态,并常常赢得变化,则使用匿名的JSON序列剂的工作确实很好:

new { CourseType.Online, CourseType.Classroom }

但是,如果你想找一些东西来处理动态或多面的、没有维修的话,那么你就可以制造出一些东西,在名称价值表上加一,并创立一个字典,以编号(无需思考)。

public static IDictionary<string, int> ConvertToMap(Type enumType)
{
  if (enumType == null) throw new ArgumentNullException("enumType");
  if (!enumType.IsEnum) throw new ArgumentException("Enum type expected", "enumType");

  var result = new Dictionary<string, int>();
  foreach (int value in Enum.GetValues(enumType))
    result.Add(Enum.GetName(enumType, value), value);

  return result;
}

<><>Edit>/strong>

If you need a JSON Serializer... I really like using JSON.NET http://james.newtonking.com/projects/json-net.aspx

问题回答

这里有:

private enum CourseType
{
    Online = 1,
    Classroom = 2
}

private void GetCourseType()
{
    StringBuilder output = new StringBuilder();

    string[] names =
        Enum.GetNames(typeof(CourseType));

    output.AppendLine("CourseType = {");
    bool firstOne = true;
    foreach (string name in names)
    {
        if (!firstOne)
            output.Append(", " + Environment.NewLine);
        output.Append(string.Format(" {0}  : {1:N0}", name, (int)Enum.Parse(typeof(CourseType), name)));

        firstOne = false;
    }
    output.AppendLine(Environment.NewLine + "}");

    // Output that you could write out to the page...
    Debug.WriteLine(output.ToString());
}

产出:

CourseType = {
 Online  : 1, 
 Classroom  : 2
}




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...