博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过反射(Reflection)实现对ref和out参数在 Portal-Builder 开源门户系统中的调用...
阅读量:4338 次
发布时间:2019-06-07

本文共 4737 字,大约阅读时间需要 15 分钟。

最近做的Portal-Builder 开源门户系统里面用到了相当多的反射(不知道这样叫中文对不对,英文是Reflection),很多人对ref和out参数如何传递使用不太清楚,在这里我来给大家介绍一下:

其实实现我们大部分时间是采用 MethodInfo.Invoke() 来实现,所以细节就在怎么使用  Type.GetMethod()这个方法了

解密:

Type 里面有 MakeByRefType()和 MakeArrayType() 两个方法, 其中 ref和out 都可以通过MakeByRefType 来实现, 因为 ref和out 其实都可以当作是把方法外的参数当作一个 object对象传入,从而实现带值返回。 MakeArratType简单介绍就是声明传入的数据为数组,如是而已。

 

下面附带一下 Portal-Builder 开源门户里面的使用ActionRunner来调用XML实现类方法调用的例子:

PortalBuilderActions.xml 代码样例

 

后台中调用和实现反射动态调用方法的代码:

public class ActionRunner     {
public static object[] Run(string codeIdentifier, object[] paramList, Guid applicationKey = default(Guid)) {
//获取Action string actionXmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "/" + Common.Global.GlobalConfig.ConfigurationFiles.PortalBuilderActionXML; if (applicationKey != default(Guid)) actionXmlFilePath = AppManager.GetIntallationPath(applicationKey); Common.Configurations.PBActions.Action runnerAction = ActionXMLParser.GetAction(codeIdentifier, actionXmlFilePath); //创建Action所有参数值列表 Dictionary
fullParamList = new Dictionary
(); if (runnerAction != null && runnerAction.Flows != null && runnerAction.Flows.Count > 0) {
foreach (Flow flow in runnerAction.Flows) {
ActionFlow(paramList, fullParamList, flow); } if (runnerAction.ReturnTrueOrFalseOnly) {
return new object[] { true }; } else {
if (runnerAction.EndResults != null && runnerAction.EndResults.Count > 0) {
List
returningValues = new List(); foreach (EndResult endResult in runnerAction.EndResults) {
var objValue = (from item in fullParamList where item.Key.ToLower() == endResult.Name.ToLower() select item.Value).FirstOrDefault(); if (objValue != null) {
endResult.Value = objValue; returningValues.Add(objValue); } } return returningValues.ToArray(); } else return null; } } else {
if (runnerAction.ReturnTrueOrFalseOnly) return new object[] { false }; else return null; }; } private static void ActionFlow(object[] paramList, Dictionary
fullParamList, Flow flow) {
if (flow is ManagerFlow) {
ManagerFlow mFlow = (ManagerFlow)flow; try {
List
paramTypes = new List
(); if (mFlow.Params != null && mFlow.Params.Count > 0) { if (mFlow.Params.Count != paramList.Length) throw new WebCiderException("实际传入的参数数量与XML中定义的参数数量不符!"); foreach (Param param in mFlow.Params) { Type type; if (!string.IsNullOrEmpty(param.ParamModifier)) { type = Type.GetType(param.ValueType, true, true).MakeByRefType(); } else type = Type.GetType(param.ValueType, true, true); paramTypes.Add(type); fullParamList.Add(param.Name, paramList[paramTypes.IndexOf(type)]); } } MethodInfo methodInfo = Type.GetType(mFlow.ClassName + "," + mFlow.AssemblyName, true, true).GetMethod(mFlow.MethodName, BindingFlags.Public | BindingFlags.Static, Type.DefaultBinder, paramTypes.ToArray(), null); if (methodInfo == null) throw new WebCiderException("获取XML定义类或类方法失败!"); object vResult = methodInfo.Invoke(null, BindingFlags.InvokeMethod, null, paramList, null); if (vResult != null) { fullParamList.Add(mFlow.Result.Name, vResult); } } catch (Exception ex) { WebCiderHelper.Logger.Error("严重系统错误:解析操作集XML时失败([" + mFlow.ClassName + "," + mFlow.AssemblyName + "->" + mFlow.MethodName + "):" + ex.Message, ex); throw new WebCiderException("严重系统错误:解析操作集XML时失败([" + mFlow.ClassName + "," + mFlow.AssemblyName + "->" + mFlow.MethodName + "):" + ex.Message, ex); } } else if (flow is ActionFlow) { ActionFlow aFlow = (ActionFlow)flow; Common.Configurations.PBActions.Action flowAction = ActionXMLParser.GetAction(aFlow.Name, AppManager.GetIntallationPath(aFlow.ApplicationKey)); if (flowAction != null) { ActionFlow(paramList, fullParamList, aFlow); } else { WebCiderHelper.Logger.Error("严重系统错误:解析操作集XML时失败 - [" + aFlow.ApplicationKey + "] 的应用程序不存在或有错无法使用."); throw new WebCiderException("严重系统错误:解析操作集XML时失败 - [" + aFlow.ApplicationKey + "] 的应用程序不存在或有错无法使用."); } } } }

以上代码引用了工作流引擎概念  仅供开发者讨论研究  有看不懂的地方还请见谅,有问题可以到 http://bbs.oelite.com 进行进一步讨论

 

 

转载于:https://www.cnblogs.com/mleader1/archive/2012/03/20/2408853.html

你可能感兴趣的文章
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>