亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如下,兩個方法,簽名唯一的區別在于參數帶Out

如下,兩個方法,簽名唯一的區別在于參數帶Out

C#
喵喵時光機 2023-01-02 18:14:19
例:public void Foo(out string value) {}public void Foo(string value){}在反射時如何區分這兩個函數比如在Type.GetMethod獲取的時候
查看完整描述

2 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

Type.GetMethod可以得到一個MethodInfo對象,MethodInfo對象有一個方法是GetParameters即得到ParameterInfo數組,ParameterInfo對象有一個屬性是IsOut。

查看完整回答
反對 回復 2023-01-06
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

Type.GetMethod可以得到一個MethodInfo對象,MethodInfo對象有一個方法是GetParameters即得到ParameterInfo數組,ParameterInfo對象有一個屬性是IsOut。

已知foo的函數原型么?如果已知的話可以用GetMethod(string, Type[])這個重載。
比如你提到的有這樣一個類:
class a
{
public void foo(string value);
public void foo(out string value);
}

如果想獲得上面那一個方法,用這個語句:

1

type.GetMethod("foo", new Type[] { typeof(string) });

如果想獲得下面那一個方法,用這個語句:

1

type.GetMethod("foo", new Type[] { typeof(string).MakeByRefType() });
1 class Program 

2 { 

3 static void Main(string[] args)
4 { 



string content = "main"; //#1 variable 

6 MethodInfo testMethod = typeof(Program).GetMethod("TestMethod", 

7 BindingFlags.Static | BindingFlags.NonPublic);
8 if (testMethod != null)
9 {
10 // Following way can not take content back.
11 //

12 testMethod.Invoke(null, new object[] { content /* #1 variable */ });
13 Console.WriteLine(content); // #1 variable, Output is: main
14 //
15
16 

17 object[] invokeArgs = new object[] { content /* #1 variable */ };
18 testMethod.Invoke(null, invokeArgs);
19 content = (string)invokeArgs[0]; // #2 variable, bypass from invoke, set to content.
20 Console.WriteLine(content); // #2 variable, Output is: test
21 }
22 }
23 

24 static void TestMethod(ref string arg)
25 {
26 arg = "test"; // #2 variable, wanna bypass to main process.
27 }
28 }

 


查看完整回答
反對 回復 2023-01-06
  • 2 回答
  • 0 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號