C#强化系列文章五:动态代码的使用(反射和动态生成类)
2008-03-07 10:41
295 查看
在软件开发尤其是框架和底层开发时,为了更灵活的控制代码,常常需要进行一些动态的操作。比如根据用户的输入等动态的调用类中的方法或者根据数据库表结构、用户要求动态的生成一些类,然后再动态的调用类中的方法。当然使用这些方式时会对性能有一点影响,具体使用过程中可以根据实际情况来定,不过一般的B/S开发中主要的瓶颈还是在数据库操作和网速方面,这点影响应该可以忽略的 class ReflTest1
这个例子中提供了三个方法和一个属性,下面的代码来动态的调用它们:
string strText = "abcd";
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Type t = typeof(ReflTest1);
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);
foreach (MethodInfo m in mi)
MethodInfo mMy = t.GetMethod("MyWrite");
if (mMy != null)
BindingFlags用来设置要取得哪些类型的方法,然后我们就可以取得这些方法来动态的调用。(当然为了可以循环的调用方法,在方法的命名方面可以自己指定一个规则)
二、动态生成类
我们可以在程序运行过程中调用.NET中提供的编译类,动态的将一段string编译成一个类,然后再通过反射来调用它
需要使用的命名空间:System.CodeDom System.CodeDom.Compiler Microsoft.CSharp System.Reflection
动态创建、编译类的代码如下:
public static Assembly NewAssembly()
private static string propertyString(string propertyName)
Assembly assembly = NewAssembly();
object Class1 = assembly.CreateInstance("DynamicClass");
ReflectionSetProperty(Class1, "aaa", 10);
ReflectionGetProperty(Class1, "aaa");
object Class2 = assembly.CreateInstance("DynamicClass");
ReflectionSetProperty(Class1, "bbb", 20);
ReflectionGetProperty(Class1, "bbb");
DynamicClass是我动态类的类名,aaa和bbb是其中的属性
ReflectionSetProperty和ReflectionGetProperty代码如下:
private static void ReflectionSetProperty(object objClass, string propertyName, int value)
private static void ReflectionGetProperty(object objClass, string propertyName)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanRead)
{
System.Console.WriteLine(info.GetValue(objClass, null));
}
}
}
这个例子中提供了三个方法和一个属性,下面的代码来动态的调用它们:
string strText = "abcd";
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Type t = typeof(ReflTest1);
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);
foreach (MethodInfo m in mi)
MethodInfo mMy = t.GetMethod("MyWrite");
if (mMy != null)
BindingFlags用来设置要取得哪些类型的方法,然后我们就可以取得这些方法来动态的调用。(当然为了可以循环的调用方法,在方法的命名方面可以自己指定一个规则)
二、动态生成类
我们可以在程序运行过程中调用.NET中提供的编译类,动态的将一段string编译成一个类,然后再通过反射来调用它
需要使用的命名空间:System.CodeDom System.CodeDom.Compiler Microsoft.CSharp System.Reflection
动态创建、编译类的代码如下:
public static Assembly NewAssembly()
private static string propertyString(string propertyName)
Assembly assembly = NewAssembly();
object Class1 = assembly.CreateInstance("DynamicClass");
ReflectionSetProperty(Class1, "aaa", 10);
ReflectionGetProperty(Class1, "aaa");
object Class2 = assembly.CreateInstance("DynamicClass");
ReflectionSetProperty(Class1, "bbb", 20);
ReflectionGetProperty(Class1, "bbb");
DynamicClass是我动态类的类名,aaa和bbb是其中的属性
ReflectionSetProperty和ReflectionGetProperty代码如下:
private static void ReflectionSetProperty(object objClass, string propertyName, int value)
private static void ReflectionGetProperty(object objClass, string propertyName)
{
PropertyInfo[] infos = objClass.GetType().GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.Name == propertyName && info.CanRead)
{
System.Console.WriteLine(info.GetValue(objClass, null));
}
}
}
相关文章推荐
- C#强化系列文章五:动态代码的使用(反射和动态生成类)
- C#强化系列文章五:动态代码的使用(反射和动态生成类)
- [导入]C#强化系列文章五:动态代码的使用(反射和动态生成类)
- C#强化系列文章五:动态代码的使用(反射和动态生成类)
- C#强化系列文章五:动态代码的使用(反射和动态生成类)
- C#强化系列文章九:代码访问安全性使用
- C#强化系列文章九:代码访问安全性使用
- 【转】C#强化系列文章九:代码访问安全性使用
- C#强化系列文章九:代码访问安全性使用
- C#强化系列文章四:匿名方法的使用
- 动态代码的使用(反射和动态生成类)
- 动态代码的使用(反射和动态生成类)
- C#强化系列文章二:在C#中使用钩子
- 动态代码的使用(反射和动态生成类)
- C#强化系列文章二:在C#中使用钩子
- C#强化系列文章三:实验分析C#中三种计时器使用异同点
- C#强化系列文章二:在C#中使用钩子
- 动态代码的使用(反射和动态生成类)
- 动态代码的使用(反射和动态生成类)
- C#强化系列文章一:ViewState使用兼谈序列化