Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Switch case语句未按预期工作_C#_.net - Fatal编程技术网

C# Switch case语句未按预期工作

C# Switch case语句未按预期工作,c#,.net,C#,.net,我写了一个小程序,基本上是在SWITCH语句上工作的。它读取switch语句中的每个case,调用并执行适当的函数。但是,我面临一个问题,当switch语句的第一个实例被调用时,所有函数都被执行。不应该是这样,因为只应该执行由特定switch case语句调用的函数。有人能告诉我我的代码有什么问题吗?发布我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using

我写了一个小程序,基本上是在SWITCH语句上工作的。它读取switch语句中的每个case,调用并执行适当的函数。但是,我面临一个问题,当switch语句的第一个实例被调用时,所有函数都被执行。不应该是这样,因为只应该执行由特定switch case语句调用的函数。有人能告诉我我的代码有什么问题吗?发布我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.ServiceModel.Security;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using Excel1.MWMClient.MWMServiceProxy;
namespace Excel1
{
    class WebServiceFunctions
    {
        private string siteID = "INDIA";
        static TemplateClient templateClient;
        static TaskClient taskClient;
        public WebServiceFunctions()
        {
            templateClient = new Excel1.MWMClient.MWMServiceProxy.TemplateClient();
            templateClient.ClientCredentials.UserName.UserName = "admin";
            templateClient.ClientCredentials.UserName.Password = "admin";
            templateClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            taskClient = new Excel1.MWMClient.MWMServiceProxy.TaskClient();
            taskClient.ClientCredentials.UserName.UserName = "admin";
            taskClient.ClientCredentials.UserName.Password = "admin";
            taskClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
        }
        public void GetAllTemplateNames(String strSite, String strRetVal)
        {
            //GetAllTemplateNames
            Console.WriteLine("Invoking GetAllTemplate method");
            Console.WriteLine("List of all templates with details");
            Console.WriteLine("-----------------------------------");
            List<Excel1.MWMClient.MWMServiceProxy.TemplateData> tData = templateClient.GetAllTemplate(strSite).ToList();
            foreach (Excel1.MWMClient.MWMServiceProxy.TemplateData data in tData)
            {
                Console.WriteLine("TemplateName:" + data.name);
                Console.WriteLine("TemplateDescription:" + data.description);
                Console.WriteLine("SiteName:" + data.site_name);
                Console.WriteLine("-----------------------------------");
            }
            Console.WriteLine("-----------------------------------");
        }
        public List<TemplateData> ReturnAllTemplateNames()
        {
            // Console.WriteLine("Invoking GetAllTemplate method");
            // Console.WriteLine("List of all templates with details");
            // Console.WriteLine("-----------------------------------");
            List<Excel1.MWMClient.MWMServiceProxy.TemplateData> tData = templateClient.GetAllTemplate(siteID).ToList();
            foreach (Excel1.MWMClient.MWMServiceProxy.TemplateData data in tData)
            {
                Console.WriteLine("TemplateName:" + data.name);
                Console.WriteLine("TemplateDescription:" + data.description);
                Console.WriteLine("SiteName:" + data.site_name);
                Console.WriteLine("-----------------------------------");
            }
            Console.WriteLine("-----------------------------------");
            return tData;
        }
        public void GetTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            Console.WriteLine("Invoking GetTaskInstanceFromTemplate method");
            Console.WriteLine("-----------------------------------");
            Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance = taskClient.GetTaskInstanceFromTemplate(siteID, data);
            if (tInstance != null)
            {
                Console.WriteLine("TaskName: " + tInstance.name);
                Console.WriteLine("TaskDescription :" + tInstance.description);
            }
            Console.WriteLine("-----------------------------------");
        }
        public TaskInstance ReturnTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            Console.WriteLine("Invoking GetTaskInstanceFromTemplate method");
            Console.WriteLine("-----------------------------------");
            Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance = taskClient.GetTaskInstanceFromTemplate(siteID, data);
            if (tInstance != null)
            {
                Console.WriteLine("TaskName: " + tInstance.name);
                Console.WriteLine("TaskDescription :" + tInstance.description);
            }
            Console.WriteLine("----------------------" + tInstance.name + "-------------");
            return tInstance;
        }
        public int CreateTask(Excel1.MWMClient.MWMServiceProxy.TaskInstance instance)
        {
            Console.WriteLine("Invoking CreateTask method");
            Console.WriteLine("-----------------------------------");
            int retVal = taskClient.CreateTask(instance);
            Console.WriteLine("Task create successfully:ID=" + retVal.ToString());
            Console.WriteLine("-----------------------------------");
            return retVal;
        }
        public void GetTaskStatus(int taskId)
        {
            Console.WriteLine("Invoking GetTaskStatus method");
            Console.WriteLine("------------------****-----------------");
            Excel1.MWMClient.MWMServiceProxy.TaskStatus taskStatus = new Excel1.MWMClient.MWMServiceProxy.TaskStatus();
            taskStatus = taskClient.GetTaskStatus(taskId);
            Console.WriteLine("Task status : " + taskStatus.taskStatus.ToString());
            Console.WriteLine("-----------------------------------");
        }
        public void PerformTaskOperation(int taskId, string operation, String reason)
        {
            Console.WriteLine("PerformTaskOperation method");
            Console.WriteLine("-----------------------------------");
            int operationStatusCode = 0;
            operationStatusCode = taskClient.PerformTaskOperation(taskId, operation, reason);
            String updateSuccess = operationStatusCode == 1 ? "update success" : "update failed";
            Console.WriteLine("Returned Operation Status Code = " + operationStatusCode + "|| Updation status message:" + updateSuccess);
        }
        //public void GetTaskHistory(int taskId, int detailLevel)
        //{
        //    Console.WriteLine("Invoking GetTaskHistory method");
        //    Console.WriteLine("------------------*****-----------------");
        //    Excel1.MWMClient.MWMServiceProxy.TaskHistory taskHistory = new Excel1.MWMClient.MWMServiceProxy.TaskHistory();
        //    taskHistory = taskClient.GetTaskHistory(taskId, detailLevel);
        //    if (taskHistory.listOfAllowedActions != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskAllowedAction allowedAction in taskHistory.listOfAllowedActions)
        //        {
        //            Console.WriteLine("Task History - allowedAction : " + allowedAction.actionName);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.listOfCustomAttributes != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskCustomAttributes customAttribute in taskHistory.listOfCustomAttributes)
        //        {
        //            Console.WriteLine("Custom Attribute : " + customAttribute.key + " | Value:" + customAttribute.value);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskinstanceEscalations != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskEscaltion taskEscaltion in taskHistory.taskinstanceEscalations)
        //        {
        //            Console.WriteLine("Task Escaltion : " + taskEscaltion.escalationLevel + " | escalationType : " + taskEscaltion.escalationType);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceUsers != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskUser taskUser in taskHistory.taskInstanceUsers)
        //        {
        //            Console.WriteLine("Task User : " + taskUser.firstName + " ,  " + taskUser.lastName);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceGroups != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskGroup group in taskHistory.taskInstanceGroups)
        //        {
        //            Console.WriteLine("Group : " + group.groupName + "| escalationLevel : " + group.escalationLevel);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskInstanceSkills != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskSkill skill in taskHistory.taskInstanceSkills)
        //        {
        //            Console.WriteLine("Skill : " + skill.skillName + "| description : " + skill.description);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.taskStatusList != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.TaskStatus taskStatus in taskHistory.taskStatusList)
        //        {
        //            Console.WriteLine("TaskStatus : " + taskStatus.taskStatus);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    if (taskHistory.userTaskInstanceList != null)
        //        foreach (Excel1.MWMClient.MWMServiceProxy.UserTaskInstance userTaskInstance in taskHistory.userTaskInstanceList)
        //        {
        //            Console.WriteLine("UserTaskInstance status: " + userTaskInstance.status);
        //            Console.WriteLine("-----------------------------------");
        //        }
        //    //taskHistory.listOfAllowedActions
        //}
        public void CreateTaskFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData data)
        {
            //taskClient.ClientCredentials.UserName.UserName = "USR";
            //taskClient.ClientCredentials.UserName.Password = "PWD";
            //taskClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            Console.WriteLine("Invoking CreateTaskFromTemplate method");
            Console.WriteLine("------------------***-----------------");
            int taskId = taskClient.CreateTaskFromTemplate(siteID, data);
            Console.WriteLine("Created task from template with TaskId: " + taskId);
            Console.WriteLine("-----------------------------------");

        }
    };
    class Program
    {
        static WebServiceFunctions wsf = new WebServiceFunctions();
        public static void Main(string[] args)
        {
            //
            //Initializing the excel sheet
            //
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTemplate.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;
            //
            //Getting row count, column count and the number of sheets 
            //
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            int numSheets = xlWorkbook.Sheets.Count;
            //
            // Iterate through the sheets. They are indexed starting at 1.
            //            
            for (int row = 2; row <= rowCount; row++)
            {
                //
                // Do something with the data in the array with a custom method.
                //
                String strRetVal = xlRange.Cells[row, 3].Value;
                String strFunctionName = xlRange.Cells[row, 4].Value;
                String strParam1 = xlRange.Cells[row, 6].Value;
                String strParam2 = xlRange.Cells[row, 7].Value;
                String strParam3 = xlRange.Cells[row, 8].Value;
                String strParam4 = xlRange.Cells[row, 9].Value;
                TemplateData tData = new TemplateData();
                TaskInstance tInstance = new TaskInstance();
                int tID = 0;
                ProcessInput(strRetVal, strFunctionName, strParam1, strParam2, strParam3, strParam4, tData, tInstance, tID);
                //creating a task from templates in one flow
                //1)Get all templates.
                List<Excel1.MWMClient.MWMServiceProxy.TemplateData> templateDataList = new List<TemplateData>();
                //bool createTaskFromTemplate_Flag = false;
                templateDataList = wsf.ReturnAllTemplateNames();
                //if (createTaskFromTemplate_Flag)
                //{
                //    foreach (TemplateData templateDataInst in templateDataList)
                //    {
                //        //We are not using the method CreateTaskFromTemplate for this application
                //        //WebServiceFunctions.CreateTaskFromTemplate(templateDataInst);
                //    }
                //}
                bool createTaskForAll = false;
                if (createTaskForAll)
                {
                    foreach (TemplateData templateDataInst in templateDataList)
                    {
                        //2)Get task instance
                        TaskInstance taskInstance = new TaskInstance();
                        taskInstance = wsf.ReturnTaskInstanceFromTemplate(templateDataInst);
                        taskInstance.scheduleActivity = new ScheduleActivity();
                        taskInstance.scheduleActivity.nextRun = new DateTime();
                        taskInstance.scheduleActivity.isRecurring = 0;
                        //over riding taskType in task
                        taskInstance.taskType = "pull";

                        //3)Create task.
                        wsf.CreateTask(taskInstance);
                    }
                }
                else 
                {
                    int numberOfTemplatesReturned = templateDataList.Count();
                    Random random = new Random();
                    int randomTemplateInList = random.Next(0, numberOfTemplatesReturned);
                    Console.WriteLine("Random number from template list:" + randomTemplateInList);
                    TemplateData templateDataInstance = new TemplateData();
                    templateDataInstance = templateDataList.ElementAt(randomTemplateInList);

                    //2)Get task instance
                    TaskInstance taskInstance = new TaskInstance();
                    taskInstance = wsf.ReturnTaskInstanceFromTemplate(templateDataInstance);

                   // 3)Create task.
                    tID = wsf.CreateTask(taskInstance);
                    wsf.GetTaskStatus(tID);
                    //int detailLevel = 2;
                   // Console.WriteLine("Task History for a newly created task would contain minimal details.");
                   // Console.WriteLine("Run method with taskId of a task which has gone through various phases to see detailed history.");
                   // wsf.GetTaskHistory(tID, detailLevel);
                }
               // Console.WriteLine("Updating status via update task status");
               //wsf.PerformTaskOperation(tID, "DELETE", "trying pauseeee");

               // Console.WriteLine("Press Enter To Exit Client Application.");
                Console.ReadLine();
            }
        }
        public static void ProcessInput(String strRetVal, String strFunctionName, /*String strParamCount,*/ String strParam1, String strParam2, String strParam3, String strParam4, TemplateData tData, TaskInstance tInstance, int tID)
        {
            switch (strFunctionName)
            {
                case "ITemplate.GetAllTemplate":
                    {
                        //ITemplate.GetAllTemplate code
                        MessageBox.Show("ITemplate.GetAllTemplate");
                        wsf.GetAllTemplateNames(strParam1, strRetVal);
                        break;
                    }
                case "ITask.GetTaskInstanceFromTemplate":
                    {
                        //ITask.GetTaskInstanceFromTemplate code
                        MessageBox.Show("ITask.GetTaskInstanceFromTemplate");
                        wsf.GetTaskInstanceFromTemplate(tData);
                        break;
                    }
                case "CreateTask":
                    {
                        //CreateTask code
                        MessageBox.Show("CreateTask");
                       wsf.CreateTask(tInstance);
                        break;
                    }
                case "UpdateDatabase":
                    {
                        //UpdateDatabase code
                        MessageBox.Show("UpdateDatabase");
                        break;
                    }
                case "GetTaskStatus":
                    {
                        //GetTaskStatus code
                        MessageBox.Show("GetTaskStatus");
                        wsf.GetTaskStatus(tID);
                        break;
                    }
                case "VerifyValue":
                    {
                        //VerifyValue code
                        MessageBox.Show("VerifyValue");
                    }
                    break;
            }
        }
    };
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统数据;
使用系统图;
使用系统组件模型;
使用System.ServiceModel.Security;
使用Excel=Microsoft.Office.Interop.Excel;
使用Microsoft.Office.Interop.Excel;
使用Excel1.MWMClient.MWMServiceProxy;
命名空间Excel1
{
类WebServiceFunctions
{
私有字符串siteID=“印度”;
静态TemplateClient TemplateClient;
静态任务客户端;
公共WebServiceFunctions()
{
templateClient=new Excel1.MWMClient.MWMServiceProxy.templateClient();
templateClient.ClientCredentials.UserName.UserName=“admin”;
templateClient.ClientCredentials.UserName.Password=“admin”;
templateClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode=X509CertificateValidationMode.None;
taskClient=new Excel1.MWMClient.MWMServiceProxy.taskClient();
taskClient.ClientCredentials.UserName.UserName=“admin”;
taskClient.ClientCredentials.UserName.Password=“admin”;
taskClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode=X509CertificateValidationMode.None;
}
public void GetAllTemplateNames(字符串strSite、字符串stretval)
{
//GetAllTemplateNames
WriteLine(“调用GetAllTemplate方法”);
Console.WriteLine(“包含详细信息的所有模板列表”);
Console.WriteLine(“-----------------------------------------”;
List tData=templateClient.GetAllTemplate(strSite.ToList();
foreach(Excel1.MWMClient.MWMServiceProxy.TemplateData数据在tData中)
{
Console.WriteLine(“TemplateName:+data.name”);
Console.WriteLine(“TemplateDescription:+data.description”);
Console.WriteLine(“站点名称:+data.site\u名称”);
Console.WriteLine(“-----------------------------------------”;
}
Console.WriteLine(“-----------------------------------------”;
}
public List ReturnAllTemplateNames()
{
//WriteLine(“调用GetAllTemplate方法”);
//Console.WriteLine(“包含详细信息的所有模板列表”);
//Console.WriteLine(“-----------------------------------------”;
List tData=templateClient.GetAllTemplate(siteID.ToList();
foreach(Excel1.MWMClient.MWMServiceProxy.TemplateData数据在tData中)
{
Console.WriteLine(“TemplateName:+data.name”);
Console.WriteLine(“TemplateDescription:+data.description”);
Console.WriteLine(“站点名称:+data.site\u名称”);
Console.WriteLine(“-----------------------------------------”;
}
Console.WriteLine(“-----------------------------------------”;
返回tData;
}
public void GetTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData数据)
{
WriteLine(“调用GetTaskInstanceFromTemplate方法”);
Console.WriteLine(“-----------------------------------------”;
Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance=taskClient.GetTaskInstanceFromTemplate(站点ID,数据);
if(tInstance!=null)
{
Console.WriteLine(“TaskName:+tInstance.name”);
Console.WriteLine(“任务描述:+tInstance.description”);
}
Console.WriteLine(“-----------------------------------------”;
}
public TaskInstance ReturnTaskInstanceFromTemplate(Excel1.MWMClient.MWMServiceProxy.TemplateData)
{
WriteLine(“调用GetTaskInstanceFromTemplate方法”);
Console.WriteLine(“-----------------------------------------”;
Excel1.MWMClient.MWMServiceProxy.TaskInstance tInstance=taskClient.GetTaskInstanceFromTemplate(站点ID,数据);
if(tInstance!=null)
{
Console.WriteLine(“TaskName:+tInstance.name”);
Console.WriteLine(“任务描述:+tInstance.description”);
}
Console.WriteLine(“-------------------------”+tInstance.name+“----------------”;
回归姿态;
}
public int CreateTask(Excel1.MWMClient.MWMServiceProxy.TaskInstance实例)
{
WriteLine(“调用CreateTask方法”);
Console.WriteLine(“-----------------------------------------”;
int retVal=taskClient.CreateTask(实例);
Console.WriteLine(“任务创建成功:ID=“+retVal.ToString());
Console.WriteLine(“-----------------------------------------”;
返回返回;
}
public void GetTaskStatus(int taskId)
{
WriteLine(“调用GetTaskStatus方法”);
Console.WriteLine(“-----------------------***--------------”;
Excel1.MWMClient.MWMServiceProxy.TaskStatus TaskStatus=新的Excel1.MWMClient.MWMServiceProxy.TaskStatus();
taskStatus=taskClient.GetTaskStatus(taskId);
Console.WriteLine(“任务状态:+taskStatus.taskStatus.ToString());
Console.WriteLine(“-----------------------------------------”;
}