服务:
[WebMethod] public string HelloWorld() { return "您好!"; }
silverlight中代码:
namespace DynamicDemo { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { CallService("DynamicDemo.Server.RefServiceSoapClient", "HelloWorld", new object[] { }, new EventHandler<DynamicDemo.Server.HelloWorldCompletedEventArgs>(aa_HelloWorldCompleted)); } void aa_HelloWorldCompleted(object sender, Server.HelloWorldCompletedEventArgs e) { MessageBox.Show("吹风说:" + e.Result); } public void CallService(string classstring, string methodstring,object[] paramstring,Delegate completedel) { Type tp = Type.GetType(classstring); EventInfo Event = tp.GetEvent(methodstring+"Completed"); MethodInfo method = tp.GetMethod(methodstring+"Async", new Type[] { }, new ParameterModifier[] { }); var chuifeng = tp.Assembly.CreateInstance(classstring); Event.AddEventHandler(chuifeng, completedel); method.Invoke(chuifeng, paramstring); } } }