我这样写,它运行到:info = Type.GetType("ClassName").GetMethod("MethodName"); 时报说对象未引用到实例这是为什么啊!public int DelMethod(String ClassName,String MethodName,String Parameter_value) { MethodInfo info = null; info = Type.GetType("ClassName").GetMethod("MethodName"); //Object myo=Type.GetType("ClassName"); int myValue=(int)info.Invoke(Type.GetType("ClassName"), new object[] { Parameter_value });
return myValue; }
"ClassName"要写全名。 比如"System.Int32"这样。
有人这样写:王乃锋(55413726) 11:48:09 using System; using System.Collections.Generic;public class MyClass { public static void Main() { myclass ob=new myclass(); System.Type mytype=ob.GetType(); System.Reflection.MethodInfo info=mytype.GetMethod("myMethod"); info.Invoke(ob,null);
} } public class myclass { public void myMethod() { Console.WriteLine("OK"); Console.Read(); } public myclass() { } } 我的这个运行时没问题的,你看看有没有可参考的,你做的跟我的不一样
正解: 类、方法作为参数传递的写法整理。(通过反射实现)需要被调用的类及方法: public class Class1 { public Class1() { // // TODO: 在此处添加构造函数逻辑 // } public int Test1(String s) { return int.Parse(s)+1; } }调用其它类和方法的类: using System.Reflection; public class ReflectMethod { public ReflectMethod() { // // TODO: 在此处添加构造函数逻辑 // } /**参数: * 对象ClassName 是一个实例的对象 * 类ClassName里面的方法MethodName名字的字符串 * 方法MethodName的输入参数,这里设定为一个输入参数,并且为字符串 * 方法MethodName的返回值设定为int类型 * * 本方法对类的传递是签名确定的,而类的方法的调用是对不确定的,即通过方法名的字符串来访问方法的 * */ public int DelMethod(Object ClassName,String MethodName,String Parameter_value) { //反射 MethodInfo info = null;
//传入类的类型 Type mytype=ClassName.GetType(); //得到调用类(ClassName)的方法 info =mytype.GetMethod(MethodName); //执行方法并得到返回值 int mya=(int)info.Invoke(ClassName, new object[] { Parameter_value }); return mya; } }主程序调用类Class1里面的方法的写法(通过类ReflectMethod的方法DelMethod) //实例化一个反射类 ReflectMethod myRM=new ReflectMethod(); //实例化一个方法类 Class1 myc=new Class1(); //通过反谢调用访求 int t=myRM.DelMethod(myc,"Test1","6023"); //显示输出 System.Diagnostics.Debug.WriteLine(t.ToString());
你要做的首先是确定类型所在的Assmebly,只有通过具体的Assembly才能找到具体的Type. 比如:Assembly asm = Assembly.GetExecutingAssembly(); Type type = asm.GetType(typename); MethodInfo info = type.GetMethod(methodName);或Assembly asm = Assembly.LoadFile(fileName); Type type = asm.GetType(typename); MethodInfo info = type.GetMethod(methodName);
--------------------------
你可以用typeof(type).FullName来得到一个类的名字,比如:string s = typeof(Form).FullName;
或
string s = typeof(Form).Name;做为参数传递一个字符串当然可以了.
这样一个代码:
MethodInfo info = null;
info=typeof(ClassName).GetMethod(MethodName); typeof()里面的参数是一个类。我想把这个参数动态的给。这应该如何实现
SomeType someType = new SomeType();
someType.GetType();
Type type = Type.GetType("ClassName");
if (type != null)
{
MethodInfo info = Type.GetType("ClassName").GetMethod("MethodName");
}
Type type = Type.GetType("ClassName");
if (type != null)
{
MethodInfo info = type.GetMethod("MethodName");
}
传字符串的话Type.GetType(typeName),typeName要写全。
时报说对象未引用到实例这是为什么啊!public int DelMethod(String ClassName,String MethodName,String Parameter_value)
{
MethodInfo info = null;
info = Type.GetType("ClassName").GetMethod("MethodName");
//Object myo=Type.GetType("ClassName");
int myValue=(int)info.Invoke(Type.GetType("ClassName"), new object[] { Parameter_value });
return myValue;
}
比如"System.Int32"这样。
using System;
using System.Collections.Generic;public class MyClass
{
public static void Main()
{
myclass ob=new myclass();
System.Type mytype=ob.GetType();
System.Reflection.MethodInfo info=mytype.GetMethod("myMethod");
info.Invoke(ob,null);
}
}
public class myclass
{
public void myMethod()
{
Console.WriteLine("OK");
Console.Read();
}
public myclass()
{
}
}
我的这个运行时没问题的,你看看有没有可参考的,你做的跟我的不一样
类、方法作为参数传递的写法整理。(通过反射实现)需要被调用的类及方法:
public class Class1
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int Test1(String s)
{
return int.Parse(s)+1;
}
}调用其它类和方法的类:
using System.Reflection;
public class ReflectMethod
{
public ReflectMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**参数:
* 对象ClassName 是一个实例的对象
* 类ClassName里面的方法MethodName名字的字符串
* 方法MethodName的输入参数,这里设定为一个输入参数,并且为字符串
* 方法MethodName的返回值设定为int类型
*
* 本方法对类的传递是签名确定的,而类的方法的调用是对不确定的,即通过方法名的字符串来访问方法的
* */
public int DelMethod(Object ClassName,String MethodName,String Parameter_value)
{
//反射
MethodInfo info = null;
//传入类的类型
Type mytype=ClassName.GetType(); //得到调用类(ClassName)的方法
info =mytype.GetMethod(MethodName); //执行方法并得到返回值
int mya=(int)info.Invoke(ClassName, new object[] { Parameter_value });
return mya;
}
}主程序调用类Class1里面的方法的写法(通过类ReflectMethod的方法DelMethod)
//实例化一个反射类
ReflectMethod myRM=new ReflectMethod(); //实例化一个方法类
Class1 myc=new Class1(); //通过反谢调用访求
int t=myRM.DelMethod(myc,"Test1","6023"); //显示输出
System.Diagnostics.Debug.WriteLine(t.ToString());
比如:Assembly asm = Assembly.GetExecutingAssembly();
Type type = asm.GetType(typename);
MethodInfo info = type.GetMethod(methodName);或Assembly asm = Assembly.LoadFile(fileName);
Type type = asm.GetType(typename);
MethodInfo info = type.GetMethod(methodName);