public class Access<T>
{
//C#泛型类
private static Type type;
//创建对象实例,调用
public static Object Invoke(String methodName,Type[] types,params Object[] values)
{
Object result = null;
//获得对象的实际类型
type=typeof(T);
try
{
//获得传入对象是否有相关方法
MethodInfo method = type.GetMethod(methodName,types);
if (method != null)
{
//有该方法则调用该方法
//Acrivator.CreateInstance创建对象的实例
result = method.Invoke(Activator.CreateInstance(type), values);
}
}
catch (Exception error)
{
Console.WriteLine(error);
result = null;
}
return result;
}
public static Object NewInstance()
{
Object result = null;
Type type = typeof(T);
try
{
result = Activator.CreateInstance(type);
}
catch (Exception error)
{
//记录日志文件
Console.WriteLine(error);
result = null;
}
return result;
}
}有注释,但是还是不知道其用法和有什么作用,求大侠详解
{
//C#泛型类
private static Type type;
//创建对象实例,调用
public static Object Invoke(String methodName,Type[] types,params Object[] values)
{
Object result = null;
//获得对象的实际类型
type=typeof(T);
try
{
//获得传入对象是否有相关方法
MethodInfo method = type.GetMethod(methodName,types);
if (method != null)
{
//有该方法则调用该方法
//Acrivator.CreateInstance创建对象的实例
result = method.Invoke(Activator.CreateInstance(type), values);
}
}
catch (Exception error)
{
Console.WriteLine(error);
result = null;
}
return result;
}
public static Object NewInstance()
{
Object result = null;
Type type = typeof(T);
try
{
result = Activator.CreateInstance(type);
}
catch (Exception error)
{
//记录日志文件
Console.WriteLine(error);
result = null;
}
return result;
}
}有注释,但是还是不知道其用法和有什么作用,求大侠详解
public static Object NewInstance()
{
Object result = null;
Type type = typeof(T);
try
{
result = Activator.CreateInstance(type);
}
Activator.CreateInstance(type)中的type必须是一个具有无参构造函数的类通过反射获得实例的这种方法的用途,让我感受最深的是对程序功能的扩展作用,无须修改程序,只需要把相应的功能编译成dll文件即可。