.net 反射,通过自定义Attribute动态调用类方法
2016-07-05 16:28
441 查看
自定义Attribute:
public class ActiveAttribute : Attribute
{
public string Msg { get; set; }
public ActiveAttribute(string msg)
{
Msg = msg;
}
}
自定义Attribute用于类:
namespace Test
{
[ActiveAttribute("ActiveDemo1")]
public class ActivityDemo : IActivity
{
public string View { set; get; }
public dynamic GetData()
{
List<string> List1 = new List<string>();
List1.Add("a");
List1.Add("b");
List<string> List2 = new List<string>();
List2.Add("c");
List2.Add("d");
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("key1", List1);
dic.Add("key2", List2);
return dic;
}
}
}
根据Attribute动态获取类:
//assemblyName是dll名,attrStr是Attibute的msg
public Type GetActivityClass(string assemblyName,string attrStr)
{
if (!String.IsNullOrEmpty(assemblyName))
{
assemblyName = path + assemblyName;
try
{
Assembly assembly = Assembly.LoadFrom(assemblyName);
Type[] ts = assembly.GetTypes();
foreach (Type t in ts)
{
var attrArr = t.GetCustomAttributes(typeof(ActiveAttribute), true);
ActiveAttribute attr = attrArr[0] as ActiveAttribute;
if (attr != null)
{
if (attr.Msg == attrStr)
{
return t;
}
}
}
}
catch (Exception e)
{
Log4NetHelp.Log.Error(e.Message);
throw new Exception(e.Message);
}
}
return null;
}
获取类并调取其方法:
string assemblyName = "Test";
Type t = assHelper.GetActivityClass(assemblyName, "ActiveDemo1");
MethodInfo mInfo = t.GetMethod("GetData");
object obj = Activator.CreateInstance(t);
dynamic result = mInfo.Invoke(obj, null);
public class ActiveAttribute : Attribute
{
public string Msg { get; set; }
public ActiveAttribute(string msg)
{
Msg = msg;
}
}
自定义Attribute用于类:
namespace Test
{
[ActiveAttribute("ActiveDemo1")]
public class ActivityDemo : IActivity
{
public string View { set; get; }
public dynamic GetData()
{
List<string> List1 = new List<string>();
List1.Add("a");
List1.Add("b");
List<string> List2 = new List<string>();
List2.Add("c");
List2.Add("d");
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("key1", List1);
dic.Add("key2", List2);
return dic;
}
}
}
根据Attribute动态获取类:
//assemblyName是dll名,attrStr是Attibute的msg
public Type GetActivityClass(string assemblyName,string attrStr)
{
if (!String.IsNullOrEmpty(assemblyName))
{
assemblyName = path + assemblyName;
try
{
Assembly assembly = Assembly.LoadFrom(assemblyName);
Type[] ts = assembly.GetTypes();
foreach (Type t in ts)
{
var attrArr = t.GetCustomAttributes(typeof(ActiveAttribute), true);
ActiveAttribute attr = attrArr[0] as ActiveAttribute;
if (attr != null)
{
if (attr.Msg == attrStr)
{
return t;
}
}
}
}
catch (Exception e)
{
Log4NetHelp.Log.Error(e.Message);
throw new Exception(e.Message);
}
}
return null;
}
获取类并调取其方法:
string assemblyName = "Test";
Type t = assHelper.GetActivityClass(assemblyName, "ActiveDemo1");
MethodInfo mInfo = t.GetMethod("GetData");
object obj = Activator.CreateInstance(t);
dynamic result = mInfo.Invoke(obj, null);
相关文章推荐
- Java反射随记
- AS3中的反射(速记) 分析
- C# 反射(Reflection)的用处分析
- C#通过反射创建自定义泛型
- 通俗易懂的C#之反射教程
- C#根据反射和特性实现ORM映射实例分析
- 深入反射生成数组的详解
- C#利用反射来判断对象是否包含某个属性的实现方法
- C#利用反射技术实现去掉按钮选中时的边框效果
- C#泛型和反射实例解析
- C# Dynamic关键字之:dynamic为什么比反射快的详解
- 传智播客学习之java 反射
- java中利用反射调用另一类的private方法的简单实例
- Java 反射之私有字段和方法详细介绍
- php使用反射插入对象示例分享
- java反射使用示例分享
- java利用反射实现动态代理示例
- 用户控件(ASCX)向网页(ASPX)传值使用反射实现
- 基于Java回顾之反射的使用分析
- C#使用反射加载多个程序集的实现方法