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