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

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

求助,關于C#反射簽名中帶Out參數的函數的問題,麻煩大神幫忙看看!

求助,關于C#反射簽名中帶Out參數的函數的問題,麻煩大神幫忙看看!

C# C
臨摹微笑 2021-11-18 15:11:51
兩個方法,簽名唯一的區別在于參數帶Out 例:public void Foo(out string value) {}public void Foo(string value){}在反射時如何區分這兩個函數比如在Type.GetMethod獲取的時候
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

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 {

5

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 }



查看完整回答
反對 回復 2021-11-23
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

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

查看完整回答
反對 回復 2021-11-23
  • 2 回答
  • 0 關注
  • 272 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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