简体   繁体   English

寻找用Enum值代替C#中的变量

[英]Looking to substitute Enum value for a variable in C#

I have an existing method which essentially hardcodes an Enum value into it. 我有一个现有的方法,基本上将一个Enum值硬编码到其中。

What I want to do, however, is to pass an Enum value to the method. 但是,我想做的是将Enum值传递给该方法。 Then, substitute the passed in value for the hardcoded field. 然后,将传入的值替换为硬编码字段。 The value that I want to substitute is: ImpactType.Item3ModerateLimited (at the end of the code). 我要替换的值是:ImpactType.Item3ModerateLimited(在代码末尾)。

Way method is right now: 方式方法现在是:

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

Definition of ImpactType field: 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,
    }

*I was thinking about doing something like below, but am getting the following error on the "Name" property. *我正在考虑做以下类似的事情,但是在“名称”属性上出现以下错误。

Cannot convert from 'string' to 'ImpactType' * 无法从“字符串”转换为“ ImpactType” *

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

See https://stackoverflow.com/a/16039343/380384 参见https://stackoverflow.com/a/16039343/380384

you want to convert an int into an enum of ImpactType . 您想将int转换为ImpactTypeenum All you need is this: 您需要的是:

ImpactType type = (ImpactType)impactType;

or 要么

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

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

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