惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - winkingzhang

Build 2016概览 WinRT开发系列之编程语言:功能和效率 WinRT开发系列之基础概念:WinRT不是…… VS2010 Extension实践(3)——实现自定义配置 [VS2010 Extension]PowerExtension.GoToDefinition VS2010 Extension实践(2) VS2010 Extension实践 Win7硬盘安装和移动硬盘访问出错的修复办法[迁移] zt. Windows Mobile开发文章收藏 [WPF]在Style中设置ToolTip的问题分析 [WPF]RadioButton在Group的Header区部分不响应鼠标选择的bug分析 WPF模式思考 (zt) How to Get IIS Web Sites Information Programmatically Visual Studio 调试器 Application Request Routing and the IIS 7.0 Web Management Service - winkingzhang Reference Resources for MOSS and WSS - winkingzhang FileSystemWatcher事件多次触发的解决方法 - winkingzhang ASP.NET Application Life Cycle - winkingzhang CAS and Native Code - winkingzhang
如何通过反射调用带有ref或者out的参数的方法[迁移]
winkingzhang · 2010-01-12 · via 博客园 - winkingzhang

原帖放在GCDN上,由于GCDN做了整合调整,现在关注产品交流等原因GCDN Blog关闭了,只好把一些有用点的搬过来了。

2009年8月13日 12:29 by winking

写这篇博客,原起今天CyljXu问我一个问题:如何通过反射调用带有ref或者out的参数的方法?想着可能其他人也会遇到这个问题,权且记录下来,以备后行者搜索到。

这个在MSDN上有解释,参考 MethodBase.Invoke方法

public Object Invoke(
Object obj,
Object[] parameters
)

参数

obj 类型:System.Object

对其调用方法或构造函数的对象。如果方法是静态的,则忽略此参数。如果构造函数是静态的,则此参数必须为 null 引用(在 Visual Basic 中为 Nothing 或定义该构造函数的类的实例。

parameters 类型: System.Object[]

调用的方法或构造函数的参数列表。这是一个对象数组,这些对象与要调用的方法或构造函数的参数具有相同的数量、顺序和类型。如果没有任何参数,则 parameters 应为 null 引用(在 Visual Basic 中为 Nothing

如果此实例所表示的方法或构造函数采用 ref 参数(在 Visual Basic 中为 ByRef ),使用此函数调用该方法或构造函数时, 该参数不需要任何特殊属性 。如果数组中的对象未用值来显式初始化,则该对象将包含该对象类型的默认值。对于引用类型的元素,该值为 null 引用(在 Visual Basic 中为 Nothing 。对于值类型的元素,该值为 0、0.0 或 false ,具体取决于特定的元素类型。

那么该如何调用并处理传值呢?请看如下示例:

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 }