期待用C#中的变量替换枚举值

时间:2015-07-27 18:49:11

标签: c# enums

我有一个现有的方法,它基本上将Enum值硬编码到其中。

但是,我想要做的是将Enum值传递给方法。然后,将传入的值替换为硬编码字段。我想要替换的值是:ImpactType.Item3ModerateLimited(在代码的末尾)。

Way方法现在就是:

strRemedyTktResponse = IssueRemedyTicket(sb1.ToString());

private string IssueRemedyTicket(string webServiceErrInfo)
        {
            string strResponse = string.Empty;

            IncidentService.HPD_IncidentInterface_Create_WSService webService = new IncidentService.HPD_IncidentInterface_Create_WSService();

            try
            {
                webService.AuthenticationInfoValue = new IncidentService.AuthenticationInfo();
                webService.AuthenticationInfoValue.userName = "smocustomer";
                webService.AuthenticationInfoValue.password = "ryder123";
                webService.Timeout = 1000 * 60;

                strResponse = webService.HelpDesk_Submit_Service(
                     new[] { ConfigurationManager.AppSettings["AssignedGroup"].ToString() }  //Assigned_Group
                    , "" //Assigned_Group_Shift_Name
                    , "Ryder System, Inc." //Assigned_Support_Company
                    , "FMS" //Assigned_Support_Organization
                    , "" //Assignee
                    , "Software" //Categorization_Tier_1
                    , "Handheld Computer" //Categorization_Tier_2
                    , "Webservice Failure" //Categorization_Tier_3
                    , "" //CI_Name
                    , "" //Closure_Manufacturer
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier3
                    , "" //Product_Model_Version
                    , "" //Closure_Product_Name
                    , "" //Department
                    , "SMO" //First_Name
                    , ImpactType.Item3ModerateLimited

ImpactType字段的定义:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:HPD_IncidentInterface_Create_WS")]
    public enum ImpactType {

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("1-Extensive/Widespread")]
        Item1ExtensiveWidespread,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("2-Significant/Large")]
        Item2SignificantLarge,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("3-Moderate/Limited")]
        Item3ModerateLimited,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("4-Minor/Localized")]
        Item4MinorLocalized,
    }

*我正在考虑做类似下面的事情,但是在&#34;名称&#34;属性。

无法转换为&#39; string&#39;到&#39; ImpactType&#39; *

private string IssueRemedyTicket(string webServiceErrInfo, int impactType)
        {
            string strResponse = string.Empty;

            IncidentService.HPD_IncidentInterface_Create_WSService webService = new IncidentService.HPD_IncidentInterface_Create_WSService();

            try
            {
                webService.AuthenticationInfoValue = new IncidentService.AuthenticationInfo();
                webService.AuthenticationInfoValue.userName = "smocustomer";
                webService.AuthenticationInfoValue.password = "ryder123";
                webService.Timeout = 1000 * 60;

                var value = "";
                if (impactType == 2)
                    value = ImpactType.Item2SignificantLarge.ToString();
                else
                    value = ImpactType.Item3ModerateLimited.ToString();

                strResponse = webService.HelpDesk_Submit_Service(
                     new[] { ConfigurationManager.AppSettings["AssignedGroup"].ToString() }  //Assigned_Group
                    , "" //Assigned_Group_Shift_Name
                    , "Ryder System, Inc." //Assigned_Support_Company
                    , "FMS" //Assigned_Support_Organization
                    , "" //Assignee
                    , "Software" //Categorization_Tier_1
                    , "Handheld Computer" //Categorization_Tier_2
                    , "Webservice Failure" //Categorization_Tier_3
                    , "" //CI_Name
                    , "" //Closure_Manufacturer
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier3
                    , "" //Product_Model_Version
                    , "" //Closure_Product_Name
                    , "" //Department
                    , "SMO" //First_Name
                    ,  ((XmlEnumAttribute)typeof(ImpactType)
                        .GetMember(value.ToString())[0]
                        .GetCustomAttributes(typeof(XmlEnumAttribute), false)[0]).Name

1 个答案:

答案 0 :(得分:1)

请参阅https://stackoverflow.com/a/16039343/380384

您想将int转换为enum ImpactType。所有你需要的是:

ImpactType type = (ImpactType)impactType;

, ...
, "SMO" //First_Name
, (ImpactType)impactType