码迷,mamicode.com
首页 > 其他好文 > 详细

C#动态方法调用

时间:2014-06-09 23:59:00      阅读:593      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

此篇将介绍C#如何在运行时动态调用方法。当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的。此篇则给你一把利剑,让动态对象的方法调用成为可能。

1.动态调用dll里的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<span style="font-family:SimSun;font-size:12px;">/// <summary>
/// 该类将被独立编入Class1.dll汇编
/// </summary>
class Class1
{
    public static string method1()
    {
        return "I am Static method (method1) in class1";
    }
    public string method2()
    {
        return "I am a Instance Method (method2) in Class1";
    }
    public string method3(string s)
    {
        return "Hello " + s;
    }
}
 
/// <summary>
/// 该类独立放入Test.exe汇编
/// </summary>
class DynamicInvoke
{
    public static void Main(string[] args)
    {
        // 动态加载汇编
        string path = "Class1.dll";
        Assembly assembly = Assembly.Load(path);
 
        // 根据类型名得到Type
        Type type = assembly.GetType("Class1");
 
        // 1.根据方法名动态调用静态方法
        string str = (string)type.InvokeMember("method1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] { });
        Console.WriteLine(str);
 
        // 2.根据方法名动态调用动态对象的成员方法
        object o = Activator.CreateInstance(type);
        str = (string)type.InvokeMember("method2", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, new object[] { });
        Console.WriteLine(str);
 
        // 3.根据方法名动态调用动态对象的有参成员方法
        object[] par = new object[] { "kunal" };
        str = (string)type.InvokeMember("method3", BindingFlags.Default | BindingFlags.InvokeMethod, null, o, par);
        Console.WriteLine(str);
 
        // 带out修饰的InvokeMember
        // System.Int32 中 public static bool TryParse(string s, out int result) 方法的调用
        var arguments = new object[] { str, null }; // 注意这里只能将参数写在外面,out参数为null也没有关系
        typeof(int).InvokeMember("TryParse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static,
             null, null, arguments);
        Console.WriteLine(arguments[1]);
    }
}</span>

2.动态加载类文件并调用方法: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<span style="font-family:SimSun;font-size:12px;">using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Microsoft.CSharp;
 
namespace _32.DynamicReflection
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            #region 内置标签方法 (动态加载)
 
            const string className = "DynamicReflection.Test"; //类名称一定要全称
            string fileName = <strong>Thread.GetDomain().BaseDirectory + "Test.cs";</strong>
 
            if (File.Exists(fileName))
            {
                var sourceFile = new FileInfo(fileName);
                CodeDomProvider provider = new CSharpCodeProvider();
                var cp = new CompilerParameters();
                cp.ReferencedAssemblies.Add("System.dll"); //添加命名空间引用 
 
                cp.GenerateExecutable = false; // 生成类库
                cp.GenerateInMemory = true; // 保存到内存
                cp.TreatWarningsAsErrors = false; // 不将编译警告作为错误
 
                // 编译
                CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile.FullName);
                if (cr.Errors.Count < 1)
                {
                    Assembly asm = cr.CompiledAssembly; // 加载
 
                    //1.调用静态方法
                    Type type = asm.GetType(className);
                    var str =(string)type.InvokeMember("SayHello1", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, new object[] {});
                    Console.WriteLine(str);
 
                    //2.调用实例方法
                    object instance = asm.CreateInstance(className);
                    str =(string)type.InvokeMember("SayHello2", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,new object[] {});
                    Console.WriteLine(str);
 
                    //3.调用带参数的方法
                    var par = new object[] {"zhangqs008"};
                    str =(string)type.InvokeMember("SayHello3", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance,par);
                    Console.WriteLine(str);
 
                    Console.Read();
                }
                else
                {
                    string msg = null;
                    for (int index = 0; index < cr.Errors.Count; index++)
                    {
                        CompilerError error = cr.Errors[index];
                        msg += "【错误" + (index + 1) + "】" + Environment.NewLine;
                        msg += "[文件] " + error.FileName + Environment.NewLine;
                        msg += "[位置] 行" + error.Line + ",列" + error.Column + Environment.NewLine;
                        msg += "[信息] " + error.ErrorText + Environment.NewLine;
                        msg += Environment.NewLine;
                    }
                    MessageBox.Show(msg, "内置方法类编译错误");
                }
            }
 
            #endregion
        }
    }
}</span>

类文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DynamicReflection
{
    public class Test
    {
        public static string SayHello1()
        {
            return "hello static method";
        }
 
        public string SayHello2()
        {
            return "hello instance method";
        }
 
        public string SayHello3(string args)
        {
            return "hello args " + args;
        }
    }
}
 

C#动态方法调用,布布扣,bubuko.com

C#动态方法调用

标签:c   style   class   blog   code   a   

原文地址:http://www.cnblogs.com/zhangqs008/p/3773735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!