动态代码的使用(反射和动态生成类)

(转自:http://www.cnblogs.com/firstyi/archive/2008/03/07/1094652.html)

在软件开发尤其是框架和底层开发时,为了更灵活的控制代码,常常需要进行一些动态的操作。比如根据用户的输入等动态的调用类中的方法或者根据数据库表结构、用户要求动态的生成一些类,然后再动态的调用类中的方法。当然使用这些方式时会对性能有一点影响,具体使用过程中可以根据实际情况来定,不过一般的B/S开发中主要的瓶颈还是在数据库操作和网速方面,这点影响应该可以忽略的
下面我就从这两个方面来说说动态代码的使用:

 

一、反射的使用

可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
需要使用的命名空间:System.Reflection
反射的作用很多,下面的例子主要是看一下怎么动态的调用类中的方法。

 1  class ReflTest1
 2     {
 3         private string _prop1;
 4 
 5         public string Prop1
 6         {
 7             get { return _prop1; }
 8             set { _prop1 = value; }
 9         }
10     
11         public void Write1(string strText)
12         {
13             Console.WriteLine("111111111:" + strText);
14         }
15         public void Write2(string strText)
16         {
17             Console.WriteLine("222222222:" + strText);
18         }
19         public void MyWrite(string strText)
20         {
21             Console.WriteLine("3333333333:" + strText);
22         }
23     }
 1 string strText = "abcd";
 2 
 3             BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
 4                 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
 5 
 6             Type t = typeof(ReflTest1);
 7             MethodInfo[] mi = t.GetMethods(flags);
 8             Object obj = Activator.CreateInstance(t);
 9 
10             foreach (MethodInfo m in mi)
11             {
12                 if (m.Name.StartsWith("Write"))
13                 {
14                     m.Invoke(obj, new object[] { strText });
15                 }
16             }
17 
18             MethodInfo mMy = t.GetMethod("MyWrite");
19             if (mMy != null)
20             {
21                 mMy.Invoke(obj, new object[] { strText });
22             }

BindingFlags用来设置要取得哪些类型的方法,然后我们就可以取得这些方法来动态的调用。(当然为了可以循环的调用方法,在方法的命名方面可以自己指定一个规则)

二、动态生成类
我们可以在程序运行过程中调用.NET中提供的编译类,动态的将一段string编译成一个类,然后再通过反射来调用它需要使用的命名空间:System.CodeDom 、System.CodeDom.Compiler 、Microsoft.CSharp System.Reflection
动态创建、编译类的代码如下:

 1 public static Assembly NewAssembly()
 2         {
 3             //创建编译器实例。   
 4             provider = new CSharpCodeProvider();
 5             //设置编译参数。   
 6             paras = new CompilerParameters();
 7             paras.GenerateExecutable = false;
 8             paras.GenerateInMemory = true;
 9 
10             //创建动态代码。   
11             StringBuilder classSource = new StringBuilder();
12             classSource.Append("public   class   DynamicClass \n");
13             classSource.Append("{\n");
14 
15             //创建属性。   
16             classSource.Append(propertyString("aaa"));
17             classSource.Append(propertyString("bbb"));
18             classSource.Append(propertyString("ccc"));
19 
20             classSource.Append("}");
21 
22             System.Diagnostics.Debug.WriteLine(classSource.ToString());
23 
24             //编译代码。   
25             CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());
26 
27             //获取编译后的程序集。   
28             Assembly assembly = result.CompiledAssembly;
29 
30             return assembly;
31         }
32 
33         private static string propertyString(string propertyName)
34         {
35             StringBuilder sbProperty = new StringBuilder();
36             sbProperty.Append(" private   int   _" + propertyName + "   =   0;\n");
37             sbProperty.Append(" public   int   " + "" + propertyName + "\n");
38             sbProperty.Append(" {\n");
39             sbProperty.Append(" get{   return   _" + propertyName + ";}   \n");
40             sbProperty.Append(" set{   _" + propertyName + "   =   value;   }\n");
41             sbProperty.Append(" }");
42             return sbProperty.ToString();
43         }

propertyString方法就是用来拼写字符串的
整个代码比较简单,主要步骤就是:1、拼写类的字符串  2、调用CSharpCodeProvider类进行编译得到程序集(assembly)
接下来就可以利用之前反射的方法来动态调用这个类中的属性了:

1 Assembly assembly = NewAssembly();
2 
3             object Class1 = assembly.CreateInstance("DynamicClass");
4             ReflectionSetProperty(Class1, "aaa", 10);
5             ReflectionGetProperty(Class1, "aaa");
6 
7             object Class2 = assembly.CreateInstance("DynamicClass");
8             ReflectionSetProperty(Class1, "bbb", 20);
9             ReflectionGetProperty(Class1, "bbb");

DynamicClass是我动态类的类名,aaa和bbb是其中的属性
ReflectionSetProperty和ReflectionGetProperty代码如下:

 1       private static void ReflectionSetProperty(object objClass, string propertyName, int value)
 2         {
 3             PropertyInfo[] infos = objClass.GetType().GetProperties();
 4             foreach (PropertyInfo info in infos)
 5             {
 6                 if (info.Name == propertyName && info.CanWrite)
 7                 {
 8                     info.SetValue(objClass, value, null);
 9                 }
10             }
11         }
 1   private static void ReflectionGetProperty(object objClass, string propertyName)
 2         {
 3             PropertyInfo[] infos = objClass.GetType().GetProperties();
 4             foreach (PropertyInfo info in infos)
 5             {
 6                 if (info.Name == propertyName && info.CanRead)
 7                 {
 8                     System.Console.WriteLine(info.GetValue(objClass, null));
 9                 }
10             }
11         }

 

 

 

 

 

 

 

posted @ 2014-03-05 16:51  摇滚的双桨  阅读(778)  评论(0编辑  收藏  举报