2017-05-14 101 views
-3

我想将一个从restful API得到的字符串值赋给我的指定值,这是一个“枚举”类变量。我试图这样做,但它不工作我猜。有人请告诉我如何正确地做到这一点? People.cs将一个字符串的值转换为枚举类变量

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Reflection; 

namespace ISTE.Models 
{ 
    public enum Designation 
    { 
     Lecturer, 
     Professor 
    } 
    public class Faculty 
    { 
     public BitmapImage _image; 
     public Uri _IconUri; 
     public string username { get; set; } 
     public string name { get; set; } 
     public string tagline { get; set; } 
     public string imagePath { get; set; } 
     public Uri IconUri 
     { 
      get; 

      set; } 

     public BitmapImage image { get; set; } 


     public string title { get; set; } 
     public string interestArea { get; set; } 
     public string office { get; set; } 
     public Uri website { get; set; } 
     public string phone { get; set; } 
     public string email { get; set; } 
     public string twitter { get; set; } 
     public string facebook { get; set; } 
     public Designation desig { get; set; } 
    } 

    public class Staff 
    { 
     public string username { get; set; } 
     public string name { get; set; } 
     public string tagline { get; set; } 
     public string imagePath { get; set; } 
     public string title { get; set; } 
     public string interestArea { get; set; } 
     public string office { get; set; } 
     public Uri website { get; set; } 
     public string phone { get; set; } 
     public string email { get; set; } 
     public string twitter { get; set; } 
     public string facebook { get; set; } 
    } 


    public class People 
    { 
     public string title { get; set; } 
     public string subTitle { get; set; } 
     public List<Faculty> faculty { get; set; } 
     public List<Staff> staff { get; set; } 

    } 
} 
------------------------------------------------------------------------ 
DataService 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Threading.Tasks; 
using ISTE.Models; 
using System.Net.Http; 

namespace ISTE.Services 
{ 
    public class PeopleDS 
    { 
     public List<Faculty> GetItemDetails() 
     { 
      People facItem = new People(); 
      Faculty fa = new Faculty(); 

      List <Faculty> fac = new List<Faculty>(); 

      try 
      { 
       using (var client = new HttpClient()) 
       { 
        //client.DefaultRequestHeaders.Add("X-API-Key", "9ef8ddfc6d254dc3a7b2cac337c6d837"); 
        string uri3 = $"https://ist.xyz.edu/api/people"; 

        var response1 = client.GetAsync(uri3).Result; 
        var content1 = response1.Content.ReadAsStringAsync().Result; 
        dynamic item1 = Newtonsoft.Json.JsonConvert.DeserializeObject(content1); 
        facItem.faculty = item1.faculty.ToObject<List<Faculty>>(); 

        fac = item1.faculty.ToObject<List<Faculty>>(); 

        foreach (Faculty fy in fac) 
        { 
         Console.WriteLine("designation \t" + fy.title); 
         fy.desig = (Designation)Enum.Parse(typeof(Designation), fy.title, true); 

         // try to parse the string as a TestEnum without throwing an exception 
         var designation = fy.desig; 
         if (Enum.TryParse(fy.title, true, out designation)) 
         { 
          // success 
         } 
         else 
         { 
          // the string isn't an element of TestEnum 
         } 

// ... 


         fy.imagePath = fy.imagePath; 
         fy.IconUri = new Uri(fy.imagePath); 
         fy.image = new System.Windows.Media.Imaging.BitmapImage(fy.IconUri); 
        } 




       } 
      } 
      catch (System.Exception ex) 
      { 
       return fac; 
      } 
      return fac; 
     } 
    } 
} 
+1

这是太多的代码,很少与代码相关。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)以及如何创建[最小,完整和可验证](http://stackoverflow.com/help/mcve)例。作为一般规则,您的问题不仅适用于您,还适用于任何可能面临与您相似问题的人。 – stybl

+0

另外,请描述它是如何“不起作用”的。你是否遇到异常?如果是这样,请显示它,并在哪一行。价值没有达到你的预期?如果是这样,请说明输入,输出和你的期望,而不是你所得到的。 –

回答

-1

如何这样的事情,把我的头顶部和未经考验:

Designation designation = (Designation) Enum.Parse(typeof(Designation), designationString); 

您希望添加错误捕获或者是使用Enum.TryParse代替,让那捕获异常你...

+0

他不是已经这么做了吗? –

+0

嗯。是的,通过代码来挖掘这些代码,就像@Sty所说的那样,对于似乎他试图用TryParse来做这件事的问题太多了。问题是如何正确地做到这一点,在我看来,我的答案符合要求。我无法测试他的代码的其余部分以寻找其他尚未描述的错误,所以我能做的最好的事情就是回答最需要我的能力的问题...... – oldcoder

+0

是啊, 我明白。他的代码已经使用了'Parse'和'TryParse'。我想我的观点是,他的问题是相当不清楚,这使得回答几乎不可能... –

-1

蒙上了字符串的枚举,你可以这样做:

SomeEnum bar; 
if (Enum.TryParse(incoming, true, out bar)) 
{ 
    // parsing succeeded 
} 
+0

虽然这是真的,他的问题已经包含你的代码... –

+0

@RufusL嗯。是的,通过代码挖掘出来,正如Sty说的那样,对于似乎他试图用TryParse来做这件事的问题太多了。问题是如何正确地做到这一点,在我看来,我的答案符合要求。我无法测试其余的代码来寻找其他尚未描述的错误,所以我能做的最好的事情就是回答最需要我的能力的问题。 – CodingYoshi

+0

对不起,我是新来的stackoverflow,并没有那么完美的问精确questions.I仍然会尝试我的水平在未来最好问清楚的方式... – Sikha

相关问题