根据类名字符串实例化类,并调用类的方法或函数 转
2012-04-18 08:33
375 查看
//获取类型信息
//如果调用其他的DLL
//System.Reflection.Assembly asmb = System.Reflection.Assembly.LoadFrom("DLL名");
// Type t = asmb.GetType("类名");
//如果是不调用其他DLL
System.Type t = System.Type.GetType("类名");
try
{
object dObj = Activator.CreateInstance(t);
//获取方法的信息
System.Reflection.MethodInfo method = t.GetMethod("方法名");
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
System.Reflection.BindingFlags flag = System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance;
//GetValue方法的参数
object[] parameters = new object[] { "参数1" };
//object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder,
parameters, null);
//取得方法返回的值
object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder,
parameters, null);
}
catch(Exception ex)
{
}
下面是我自己写的一个Reflection类,可以根据类名,方法名执行方法
[csharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace SearchFromDB
{
class Reflection
{
string ClassName="";
Type clstype;
public Reflection(string ClassName)
{
this.ClassName = ClassName;
}
public Reflection()
{
}
/// <summary>
/// 实例对象时需要指定类名
/// </summary>
public object GetClassInstance(string assembly ,string NameSpace)
{
//assembly为程序集名称,NameSpace为命名空间
clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, ".", this.ClassName));
if (clstype == null)
return null;
object obj = (object)Activator.CreateInstance(clstype);
return obj;
}
/// <summary>
/// 实例对象时不用指定类名
/// </summary>
public object GetClassInstance(string assembly,string NameSpace,string classname)
{
ClassName = classname;
clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, ".", classname));
if (clstype == null)
return null;
object obj = (object)Activator.CreateInstance(clstype);
return obj;
}
/// <summary>
/// 执行类的静态方法
/// </summary>
/// <param name="methodname">
/// 类的方法名
/// </param>
/// <param name="parameters">
/// 方法的参数类型
/// </param>
/// <param name="methodtype">
/// 方法的参数
/// </param>
public object GetMethod(string methodname, Type[] methodtype, object[] parameters)
{
// methodtype.SetValue(typeof(string),1);
System.Reflection.MethodInfo pMethod = clstype.GetMethod(methodname, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public, null, methodtype, null);
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
//System.Reflection.BindingFlags flag = BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public;
object returnValue = pMethod.Invoke(null, parameters);
//string returnValue = pMethod.Invoke(clsObj, flag, Type.DefaultBinder, parameters,null).ToString();
return returnValue;
}
}
}
//如果调用其他的DLL
//System.Reflection.Assembly asmb = System.Reflection.Assembly.LoadFrom("DLL名");
// Type t = asmb.GetType("类名");
//如果是不调用其他DLL
System.Type t = System.Type.GetType("类名");
try
{
object dObj = Activator.CreateInstance(t);
//获取方法的信息
System.Reflection.MethodInfo method = t.GetMethod("方法名");
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
System.Reflection.BindingFlags flag = System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance;
//GetValue方法的参数
object[] parameters = new object[] { "参数1" };
//object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder,
parameters, null);
//取得方法返回的值
object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder,
parameters, null);
}
catch(Exception ex)
{
}
下面是我自己写的一个Reflection类,可以根据类名,方法名执行方法
[csharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace SearchFromDB
{
class Reflection
{
string ClassName="";
Type clstype;
public Reflection(string ClassName)
{
this.ClassName = ClassName;
}
public Reflection()
{
}
/// <summary>
/// 实例对象时需要指定类名
/// </summary>
public object GetClassInstance(string assembly ,string NameSpace)
{
//assembly为程序集名称,NameSpace为命名空间
clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, ".", this.ClassName));
if (clstype == null)
return null;
object obj = (object)Activator.CreateInstance(clstype);
return obj;
}
/// <summary>
/// 实例对象时不用指定类名
/// </summary>
public object GetClassInstance(string assembly,string NameSpace,string classname)
{
ClassName = classname;
clstype = Assembly.Load(assembly).GetType(string.Concat(NameSpace, ".", classname));
if (clstype == null)
return null;
object obj = (object)Activator.CreateInstance(clstype);
return obj;
}
/// <summary>
/// 执行类的静态方法
/// </summary>
/// <param name="methodname">
/// 类的方法名
/// </param>
/// <param name="parameters">
/// 方法的参数类型
/// </param>
/// <param name="methodtype">
/// 方法的参数
/// </param>
public object GetMethod(string methodname, Type[] methodtype, object[] parameters)
{
// methodtype.SetValue(typeof(string),1);
System.Reflection.MethodInfo pMethod = clstype.GetMethod(methodname, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public, null, methodtype, null);
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
//System.Reflection.BindingFlags flag = BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public;
object returnValue = pMethod.Invoke(null, parameters);
//string returnValue = pMethod.Invoke(clsObj, flag, Type.DefaultBinder, parameters,null).ToString();
return returnValue;
}
}
}
相关文章推荐
- 根据类名字符串实例化类,并调用类的方法或函数
- 根据类名字符串创建类名并且调用类里的方法
- java反射:根据给定类名实例化类并调用其方法
- 根据字符串名称动态调用Python的函数和对象方法
- 根据类名和函数名字符串调用相关函数
- 根据类名字符串创建类名并且调用类里的方法
- 根据字符串动态调用对象的方法
- Java如何根据类名和方法名创建类的实例,并调用对应方法?
- 非static成员函数通过类名::来调用,空指针调用成员方法不出错!
- 通过函数调用,获取字符串的方法
- Nodejs根据字符串调用对象方法
- 根据字符串动态调用对象的方法
- [转]C# 关于反射类[System.Reflection] 根据类名 动态调用 类方法
- python 调用 DLL 的函数,返回的字符串数组的食用方法
- JavaScript中的常见函数字符串可以调用的方法(2)
- 正则表达式去除字符串左右空格函数 调用方法是,str.Trim();
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- NSSelectorFromString 的使用,根据字符串来调用相应名字的方法
- python 调用 DLL 的函数,返回的字符串数组的食用方法
- [NET]Net中的反射使用入门(根据类名和函数名,生成和调用对象的成员函数) (转)