2009-08-04 32 views
7

Làm cách nào để chuyển một phương thức làm đối số? Tôi làm điều này tất cả các thời gian trong Javascript và cần phải sử dụng các phương pháp vô danh để vượt qua params. Làm thế nào để làm điều đó trong C#?Vượt qua một phương pháp làm đối số

protected void MyMethod(){ 
    RunMethod(ParamMethod("World")); 
} 

protected void RunMethod(ArgMethod){ 
    MessageBox.Show(ArgMethod()); 
} 

protected String ParamMethod(String sWho){ 
    return "Hello " + sWho; 
} 

Trả lời

13

Delegates cung cấp cơ chế này. Cách nhanh chóng để thực hiện việc này trong C# 3.0 cho ví dụ của bạn là sử dụng Func<TResult> trong đó TResultstring và lambdas.

Mã của bạn sau đó sẽ trở thành:

protected void MyMethod(){ 
    RunMethod(() => ParamMethod("World")); 
} 

protected void RunMethod(Func<string> method){ 
    MessageBox.Show(method()); 
} 

protected String ParamMethod(String sWho){ 
    return "Hello " + sWho; 
} 

Tuy nhiên, nếu bạn đang sử dụng C# 2.0, bạn có thể sử dụng một đại biểu vô danh thay vì:

// Declare a delegate for the method we're passing. 
delegate string MyDelegateType(); 

protected void MyMethod(){ 
    RunMethod(delegate 
    { 
     return ParamMethod("World"); 
    }); 
} 

protected void RunMethod(MyDelegateType method){ 
    MessageBox.Show(method()); 
} 

protected String ParamMethod(String sWho){ 
    return "Hello " + sWho; 
} 
+0

điều này sẽ không biên dịch. RunMethod mất một Func bạn đang đi đến nó Func

+0

@StanR: Nó được chỉnh sửa cho phù hợp. –

+0

+1 để hiển thị thay thế C# 2.0. –

9

ParamMethod của bạn là loại Func < String, String> vì phải mất một đối số chuỗi và trả về một chuỗi (lưu ý rằng mục cuối cùng trong các dấu ngoặc nhọn là kiểu trả về).

Vì vậy, trong trường hợp này, mã của bạn sẽ trở thành một cái gì đó như thế này:

protected void MyMethod(){ 
    RunMethod(ParamMethod, "World"); 
} 

protected void RunMethod(Func<String,String> ArgMethod, String s){ 
    MessageBox.Show(ArgMethod(s)); 
} 

protected String ParamMethod(String sWho){ 
    return "Hello " + sWho; 
} 
+0

Cảm ơn câu trả lời. Tôi nhận được một lỗi biên dịch '... RunMethod (Func , String) có một số đối số không hợp lệ. – Praesagus

+1

Bạn đang sử dụng phiên bản C# nào? –

4
protected String ParamMethod(String sWho) 
{ 
    return "Hello " + sWho; 
} 

protected void RunMethod(Func<string> ArgMethod) 
{ 
    MessageBox.Show(ArgMethod()); 
} 

protected void MyMethod() 
{ 
    RunMethod(() => ParamMethod("World")); 
} 

Đó () => là quan trọng. Nó tạo ra một số vô danh Func<string> từ Func<string, string> đó là ParamMethod.

+0

+1 đây là một ví dụ tốt, mặc dù hơi phức tạp –

0
protected delegate String MyDelegate(String str); 

protected void MyMethod() 
{ 
    MyDelegate del1 = new MyDelegate(ParamMethod); 
    RunMethod(del1, "World"); 
} 

protected void RunMethod(MyDelegate mydelegate, String s) 
{ 
    MessageBox.Show(mydelegate(s)); 
} 

protected String ParamMethod(String sWho) 
{ 
    return "Hello " + sWho; 
} 
Các vấn đề liên quan