简体   繁体   English

如何使用 SelectToken 将 c# 中的字符串转换为枚举

[英]How to convert string to enum in c# with SelectToken

Please refer to the below-given code snippet请参考下面给出的代码片段

foreach (var invoiceDescription in qbInvoiceLineArray)
{
    Line lineDescription = new Line();
    lineDescription.Description = Convert.ToString((string)invoiceDescription.SelectToken(QBConfig.InvoiceDescription));
    invoice.Line.Add(lineDescription);             
} 
    
public class QbInvoiceViewModel
{
    public decimal Balance { get; set; }       
    public List<LinkedTxn> LinkedTxn { get; set; }
    public DateTime DueDate { get; set; }
    public DateTime TxnDate { get; set; }
    public Decimal TotalAmt { get; set; }
    public List<Line> Line { get; set; }    
}
    
public class Line
{
    public string Description { get; set; }
    public LineDetailTypeEnum DetailType { get; set; }
}
    
public enum LineDetailTypeEnum
{
    SalesItemLineDetail
}

This line of code这行代码

lineDescription.Description = Convert.ToString((string)invoiceDescription.SelectToken(QBConfig.InvoiceDescription));

can be used to convert string to string in c#.可用于在 c# 中将字符串转换为字符串。 This type of code can be used to convert string to int also in c#.这种类型的代码也可以在 c# 中用于将字符串转换为 int。 But how this type of code can be used to convert string to enum?但是如何使用这种类型的代码将字符串转换为枚举呢?

You don't use Convert.ToString to strings to other things, you use Convert.ToXxx你不使用 Convert.ToString 到其他东西的字符串,你使用 Convert.ToXxx

If invoiceDescription.SelectToken(QBConfig.InvoiceDescription) is returning you a string (or an object that is actually a string, as your cast might indicate) of the enum member name or value, and you want it converting to a particular enum, you should Enum.Parse<T> it, like var e = Enum.Parse<InvoiceToken>((string)invoiceDescription.SelectToken(QBConfig.InvoiceDescription)) , of course replacing InvoiceToken with the actual name of your enum如果invoiceDescription.SelectToken(QBConfig.InvoiceDescription)您返回枚举成员名称或值的字符串(或实际上是字符串的 object,正如您的演员可能指出的那样),并且您希望它转换为特定枚举,您应该Enum.Parse<T>它,就像var e = Enum.Parse<InvoiceToken>((string)invoiceDescription.SelectToken(QBConfig.InvoiceDescription)) ,当然用你的枚举的实际名称替换 InvoiceToken

If invoiceDescription.SelectToken(QBConfig.InvoiceDescription) is returning you an int that is the enum member value (not the name) you can also alternatively Convert.ToInt32() it and then cast it to the enum type;如果invoiceDescription.SelectToken(QBConfig.InvoiceDescription)您返回一个 int,它是枚举成员值(不是名称),您也可以将其转换为Convert.ToInt32()然后将其转换为枚举类型; any int can be cast to an enum任何 int 都可以转换为枚举

MSDN for enum parse用于枚举解析的MSDN

(It's probably also worth noting that Enum.Parse<T> is more recent than other forms; if youre on an older version of .NET you're looking at something more like (T)Enum.Parse(typeof(T), string_here) ) (可能还值得注意的是, Enum.Parse<T>比其他 forms 更新;如果您使用的是旧版本的 .NET,那么您正在查看的内容更像(T)Enum.Parse(typeof(T), string_here) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM