版权声明:Till good is better, but better best !My trying hard will go on!Please wait and see! https://blog.csdn.net/u012761373/article/details/79659374
在用反射对方法进行调用时,需要对类的方法进行解析
#region 了解方法分属性和调用
Person person = new Person() { Name = "郭**", Age = 8 };
Type t1 = typeof(Person);
Type t2 = person.GetType();
string strMsg = "";
//获取事件
EventInfo[] events = t2.GetEvents();
foreach (EventInfo item in events)
{
strMsg += "</ b>" + item.Name;
}
//获取公共字段(Public Field)
FieldInfo[] fields = t2.GetFields();
foreach (FieldInfo item in fields)
{
strMsg += "</ b>" + item.Name;
}
//获取类的所有方法
MethodInfo[] methods = t2.GetMethods();
foreach (MethodInfo item in methods)
{
strMsg += "</ b>" + item.Name;
}
#endregion
通过上面的类转换我们可以看到类对应的属性和方法和事件等信息。
下面我们讲讲反射调用的原理也和这个差不多代码如下:
类库 Common:
namespace Common
{
public class CommFun
{
#region 通过配置反射调用
/// <summary>
/// 通过配置反射调用
/// </summary>
/// <param name="iDb"></param>
///<param name="strReflectConfig">RealEstateConfig配置的函数名</param>
///<param name="arrObj">参数数组</param>
public object ReflectionInvoke(string strReflectConfig, object[] arrObj)
{
//通过配置反射调用
string strReflect = GetConfig(strReflectConfig);
if (!string.IsNullOrEmpty(strReflect))
{
string[] arrReflect = strReflect.Split('@');
if (arrReflect.Length == 3)
{
//Type type = Assembly.LoadFile(HttpContext.Current.Server.MapPath(@"~/bin/" + arrReflect[0])).GetType(arrReflect[1], false);
string path = System.AppDomain.CurrentDomain.BaseDirectory + arrReflect[0];//+ @"~/bin/Debug/"
Type type = Assembly.LoadFile(path).GetType(arrReflect[1], false);
MethodInfo method = type.GetMethod(arrReflect[2]);
object obj = Activator.CreateInstance(type);
return method.Invoke(obj, arrObj);
}
}
return null;
}
#endregion
#region 取根目录AppConfig.config配置文件
/// <summary>
/// 取根目录AppConfig.config配置文件
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public string GetConfig(string strKey)
{
System.Collections.IDictionary dic = (System.Collections.IDictionary)System.Configuration.ConfigurationManager.GetSection("AppConfig");
if (dic[strKey] == null)
{
return "";
}
return dic[strKey].ToString();
}
#endregion
}
}
配置 App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="AppConfig" type="System.Configuration.DictionarySectionHandler"></section>
</configSections>
<AppConfig configSource="AppConfig.config">
</AppConfig>
</configuration>
配置表 AppConfig.config:
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<AppConfig>
<!--反射调用-->
<add key="ReflectionInvoke" value="[email protected]@FunTwo" />
</AppConfig>
Main方法
CommFun comFun = new CommFun();
object[] arrObj = new object[2];
arrObj[0] = "One";
arrObj[1] = "Two";
this.lblResult1.Text = comFun.ReflectionInvoke("ReflectionInvoke", arrObj).ToString();
结果:FunOne:One|Two
希望对你有帮助:
原理参考网址:
网址一:https://www.cnblogs.com/DrHao/p/5401006.html
网址二:https://www.cnblogs.com/xuwendong/p/7575181.html
如果配置AppConfig.config出现异常:
参考资源:配置异常处理