现在的位置: 首页 > 综合 > 正文

C#中反射调用带out参数的方法

2012年01月07日 ⁄ 综合 ⁄ 共 836字 ⁄ 字号 评论关闭

using System;
using System.Reflection;

namespace ConsoleApplication1
{
    public class TestAssembly
    {
        public TestAssembly()
        {
            String str = "aa";
            object[] parmsObj = new object[] { str };

            Assembly assembly = Assembly.GetExecutingAssembly();
            Type actionType = assembly.GetType("ConsoleApplication1.TestClass");
            MethodInfo method = tp.GetMethod("test", new Type[] { Type.GetType("System.String&") });
            object obj = Activator.CreateInstance(actionType.FullName);
            method.Invoke(obj, parmsObj);
            Console.WriteLine(parmsObj[0]);
        }
    }

    public class TestClass
    {
        public void test(out string str)
        {
            str = "test1";
        }

        public void test(string str)
        {
            str = "test2";
        }
    }
}

链接资源: http://support.microsoft.com/default.aspx?scid=kb%3bzh-cn%3b815211 

抱歉!评论已关闭.