我需要在服务端传输一段方法给客户端调用,但参数是客户端的,我做了个实验,没有成功,望告人指教!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                BinaryFormatter a = new BinaryFormatter();
                System.IO.MemoryStream b = new System.IO.MemoryStream();
                a.Serialize(b, new Action<string>(delegate { method(null); }));
                b.Position = 0;
                dynamic c = a.Deserialize(b);
                c.DynamicInvoke(new object[] { "ds" }); //我想传参数进去,但没用
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void method(string t)
        {
            MessageBox.Show(t);
        }
    }
}

解决方案 »

  1.   

    a.Serialize(b, new Action<string>(delegate { method(null); }));
    =>
    a.Serialize(b, new Action<string>(_ => method(_)));
      

  2.   

    a.Serialize(b, new Action<string>(t => method(t)));更容易理解些
      

  3.   


    t => method(t)敢问这属于哪块知识了
      

  4.   

    其实这样写更容易理解:
    a.Serialize(b, new Action<string>(method));
      

  5.   

    或者a.Serialize(b, new Action<string>(delegate(string s) { method(s); }));
    BTW: t => method(t) 是 lambada 表达式, 有些语言里叫Closure
    t => method(t) 意思差不多等于 var f(var t) { return method(t); } 其中的 var 由编译器根据语境来推理出适当的类型。如果第一个var推导结果为 void, 则后边 的return 去掉
      

  6.   

    delegate无法序列化并进行远程调用。
    如果你要远程调用,用 Remoting, 或者 WCF 
      

  7.   

    a.Serialize(b, new Action<string>(_ => method(_)));
    这写法是什么个意思?的确可以了!
      

  8.   


    晕,是lambada吧...
    你写个"_"还真没看出来- -