我想将一个从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;
}
}
}
这是太多的代码,很少与代码相关。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)以及如何创建[最小,完整和可验证](http://stackoverflow.com/help/mcve)例。作为一般规则,您的问题不仅适用于您,还适用于任何可能面临与您相似问题的人。 – stybl
另外,请描述它是如何“不起作用”的。你是否遇到异常?如果是这样,请显示它,并在哪一行。价值没有达到你的预期?如果是这样,请说明输入,输出和你的期望,而不是你所得到的。 –