C# 关于反射类[System.Reflection] 根据类名 动态调用 类方法
2009-04-07 19:31
676 查看
/// <summary>
/// Execute the function of the class
/// </summary>
/// <param name="ProjectName">the project name of the class </param>
/// <param name="spaceName">the naming space name of the class</param>
/// <param name="className">the name of the class</param>
/// <param name="functionName">the function name which need to execute</param>
/// <param name="parametersType">the type of the parameters</param>
/// <param name="parametersValue">the value of the parameters</param>
/// <param name="page">if you want to execute the function of the page ,set this parameter E.g. : this.Page</param>
/// <returns>object</returns>
public static object OperateFuction(string ProjectName, string spaceName, string className, string functionName, ArrayList parametersTypeStr, object[] parametersValue, out string Error,Page page)
{
//define the object which return
object returnObj;
//Init
returnObj = null;
Error = string.Empty;
System.Reflection.Assembly asm;
System.Type type;
//the function in this page which not need the space name and project name
if (!"".Equals(className))
{
type = Type.GetType(className);
}else{
type = page.GetType();
}
if (type == null)
{
string loadFile = string.Empty;
loadFile = !"".Equals(ProjectName) ? ProjectName + ".dll" : "__code";
if (!"__code".Equals(loadFile))
{
//get the class which in the other project
string BasePath = System.AppDomain.CurrentDomain.BaseDirectory;
BasePath = BasePath.Replace(@"\\", @"\");
string ProjectDllPath =BasePath+ @"Bin\" + ProjectName + ".dll";
try
{
asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
}
catch (System.Exception e)
{
ProjectDllPath = BasePath + ProjectName + ".dll";
asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
}
}
else
{
//get the class which in the App_Code file
asm = System.Reflection.Assembly.Load(loadFile);
}
className = !"".Equals(spaceName) ? spaceName + "." + className : className;
//get the class which needs
type = asm.GetType(className);
}
if (type != null)
{
//get the object of the class
Object obj = Activator.CreateInstance(type);
// define the parameters of the function
System.Type[] parametersType = new Type[parametersTypeStr.Count];
int i = 0;
foreach (string str in parametersTypeStr)
{
parametersType[i] = System.Type.GetType(str);
i++;
}
//get the function of the class
System.Reflection.MethodInfo method = type.GetMethod(functionName, parametersType);
if (method != null)
{
try
{
// Execute the function of the class
returnObj = method.Invoke(obj, parametersValue);
}
catch (System.Exception e)
{
Error = e.Message;
}
}
}
return returnObj;
}
/// Execute the function of the class
/// </summary>
/// <param name="ProjectName">the project name of the class </param>
/// <param name="spaceName">the naming space name of the class</param>
/// <param name="className">the name of the class</param>
/// <param name="functionName">the function name which need to execute</param>
/// <param name="parametersType">the type of the parameters</param>
/// <param name="parametersValue">the value of the parameters</param>
/// <param name="page">if you want to execute the function of the page ,set this parameter E.g. : this.Page</param>
/// <returns>object</returns>
public static object OperateFuction(string ProjectName, string spaceName, string className, string functionName, ArrayList parametersTypeStr, object[] parametersValue, out string Error,Page page)
{
//define the object which return
object returnObj;
//Init
returnObj = null;
Error = string.Empty;
System.Reflection.Assembly asm;
System.Type type;
//the function in this page which not need the space name and project name
if (!"".Equals(className))
{
type = Type.GetType(className);
}else{
type = page.GetType();
}
if (type == null)
{
string loadFile = string.Empty;
loadFile = !"".Equals(ProjectName) ? ProjectName + ".dll" : "__code";
if (!"__code".Equals(loadFile))
{
//get the class which in the other project
string BasePath = System.AppDomain.CurrentDomain.BaseDirectory;
BasePath = BasePath.Replace(@"\\", @"\");
string ProjectDllPath =BasePath+ @"Bin\" + ProjectName + ".dll";
try
{
asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
}
catch (System.Exception e)
{
ProjectDllPath = BasePath + ProjectName + ".dll";
asm = System.Reflection.Assembly.LoadFrom(ProjectDllPath);
}
}
else
{
//get the class which in the App_Code file
asm = System.Reflection.Assembly.Load(loadFile);
}
className = !"".Equals(spaceName) ? spaceName + "." + className : className;
//get the class which needs
type = asm.GetType(className);
}
if (type != null)
{
//get the object of the class
Object obj = Activator.CreateInstance(type);
// define the parameters of the function
System.Type[] parametersType = new Type[parametersTypeStr.Count];
int i = 0;
foreach (string str in parametersTypeStr)
{
parametersType[i] = System.Type.GetType(str);
i++;
}
//get the function of the class
System.Reflection.MethodInfo method = type.GetMethod(functionName, parametersType);
if (method != null)
{
try
{
// Execute the function of the class
returnObj = method.Invoke(obj, parametersValue);
}
catch (System.Exception e)
{
Error = e.Message;
}
}
}
return returnObj;
}
相关文章推荐
- [转]C# 关于反射类[System.Reflection] 根据类名 动态调用 类方法
- C#反射动态调用dll中的方法,并返回结果[转]
- C#,动态加载DLL,通过反射,调用参数,方法,窗体
- C# 使用反射动态调用泛型方法
- Java反射 根据不同方法名动态调用不同的方法
- C#反射动态调用dll中的方法,并返回结果
- C#反射:获取自定义类,动态调用方法
- Java反射:根据方法名动态调用方法,解决商品动态属性取值问题。
- C#反射动态调用窗体,动态实例化窗体的方法
- C#实现反射调用动态加载的DLL文件中的方法
- C#反射动态调用dll中的方法,并返回结果
- C# 使用反射实现根据名称动态创建窗体的几种方法
- C#反射动态调用dll中的方法,并返回结果
- C#实现反射调用动态加载的DLL文件中的方法
- C#实现反射调用动态加载的DLL文件中的方法
- c#关于反射与特性《一》---动态调用成员
- C#实现反射调用动态加载的DLL文件中的方法
- C#实现反射调用动态加载的DLL文件中的方法
- C# 通过反射类动态调用DLL方法
- C#反射动态调用dll中的方法