我有一个webservice装在10台不同web机器上。配置如下: 
web.config  
....  
<appSettings>  
<add key="web1.Service" value="http://192.168.1.1/Service.asmx"/>  
<add key="web2.Service" value="http://192.168.1.2/Service.asmx"/>  
<add key="web3.Service" value="http://192.168.1.3/Service.asmx"/>  
......... 
</appSettings>  
....  
其实在这10台web机上的webservice是完全一样的。 在程序中我想动态调用,怎么写个小函数实现下面的功能,怎么写? 
============== 
int i=外界接受变量; 
if(i==1) 
web1.Service().helloworld(); 
elseif(i==2) 
web2.Service().helloworld(); 
elseif(i==3) 
web3.Service().helloworld(); 
...... 
else 
web10.Service().helloworld(); 
或者 
string str; 
if(i==1) 
str=web1.Service().doit(args,args2); 
elseif(i==2) 
str=web2.Service().doit(args,args2); 
elseif(i==3) 
str=web3.Service().doit(args,args2); 
...... 
else 
str=web10.Service().doit(args,args2); 
================ 我想用个fun来处理这么多if和else 
public xxx  fun(int i) 

这个函数要根据 传入值i来调不同的webxxx.Service(),还要带上其调用的方法名和参数 有返回值的也要能接受到。 

解决方案 »

  1.   


    protected string fun(int i)
        {
          string aa
            switch (i)
            {
                case 1:
                    aa=web1.Service().doit(args,args2); 
                    return aa;
                    break;
                case 2:
                    aa=web1.Service().doit(args,args2); 
                    return aa;
                    break;
                default:
                    aa=web1.Service().doit(args,args2);
                    return aa;
                    break;
            }
      
      

  2.   

    Service()里有30几个方法,doit(args,args2);只是其中一个,还有helloworld()呢。不可能全写死吧。
      

  3.   


    protected string fun(int i)
        {
          string aa = string.Empty;
            switch (i)
            {
                case 1:
                    aa=web1.Service().doit(args,args2); 
                    break;
                case 2:
                    aa=web1.Service().doit(args,args2); 
                    break;
                default:
                    aa=web1.Service().doit(args,args2);
                    break;
            }
           return aa;
      

  4.   

    可以执行一个字符串,下面是例子    string test = "MyProc(10)";
      string name = test.Substring(0, test.IndexOf('('));
      string arg = test.Replace(name + "(", "").TrimEnd(')');  MethodInfo mi = typeof(Class1).GetMethod(name);
      if (mi != null)
      {
        object[] os = new object[] { Convert.ToInt16(arg) };
        mi.Invoke(null, os);
      }
     public static void MyProc(int n)
     {
         Console.Write(n);
         Console.WriteLine("");
     }
      

  5.   

    参考一下吧.
    using System;
    using System.Reflection;
    using System.Security; class MyClass
    {
        public int myInt = 0;
        public string myString = null;
        public MyClass()
        {
        }
        public void Myfunction(int i)
        {
        } 
    }
    class Type_GetMethod
    {
        public static void Main()
        {
            try
            {         
                MyClass MyObject = new MyClass();
                MethodInfo myMethodInfo;             // Get the type of MyClass.
                Type myType = MyObject.GetType(); 
            
                // Get the method information for MyFunction. 
                myMethodInfo = myType.GetMethod("Myfunction");
             
                // Get the parameters for Myfunction.
                ParameterInfo[] myParameters = myMethodInfo.GetParameters();
        
                Console.WriteLine( "\nThe parameters of the method Myfunction of class MyClass are :\n"); 
             
                // Display the position and type of the parameters.
                for(int i = 0; i < myParameters.Length; i++)
                    Console.WriteLine("The data type of parameter {0} is {1}.", 
                        myParameters[i].Position + 1, myParameters[i].ParameterType);
            }
            catch (SecurityException e)
            {
                Console.WriteLine("SecurityException: " + e.Message ); 
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message ); 
            }      
        }
    }