日期:2014-05-18  浏览次数:20450 次

帮我看看这段小代码怎么写?
我有一个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(),还要带上其调用的方法名和参数 有返回值的也要能接受到。 



------解决方案--------------------
C# code

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;
        }

------解决方案--------------------
C# code

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;

------解决方案--------------------
可以执行一个字符串,下面是例子

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("");
 }

------解决方案--------------------
那只能用反射做了
------解决方案--------------------
估计要反射了..
------解决方案--------------------
参考一下吧.

C# code

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 M