using System;
using System.Reflection;
using System.Collections.Generic;
public class A
{
private void method () {Console.WriteLine( "I am a private method in class A ");}
}
public class B
{
public void method () {Console.WriteLine( "I am a public method in class B ");}
}
public class MyClass
{
public static void Main()
{
RL(); Console.WriteLine ( "\nReflection.MethodInfo\n ");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type t = typeof(A);
MethodInfo mi1 = t.GetMethod( "method ", BindingFlags.NonPublic | BindingFlags.Instance); mi1.Invoke(MyA,null); Type tt = typeof(B);
MethodInfo mi2 = tt.GetMethod( "method ");
mi2.Invoke(MyB, null);
RL();
} #region Helper methods private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
} private static void RL()
{
Console.ReadLine();
} private static void Break()
{
System.Diagnostics.Debugger.Break();
} #endregion
}
调试的时候显示
mi1.Invoke(MyA,null);
未处理的nullreferenceexception
未将对象引用设置到对象的实例
希望大家赐教
using System.Reflection;
using System.Collections.Generic;
public class A
{
private void method () {Console.WriteLine( "I am a private method in class A ");}
}
public class B
{
public void method () {Console.WriteLine( "I am a public method in class B ");}
}
public class MyClass
{
public static void Main()
{
RL(); Console.WriteLine ( "\nReflection.MethodInfo\n ");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type t = typeof(A);
MethodInfo mi1 = t.GetMethod( "method ", BindingFlags.NonPublic | BindingFlags.Instance); mi1.Invoke(MyA,null); Type tt = typeof(B);
MethodInfo mi2 = tt.GetMethod( "method ");
mi2.Invoke(MyB, null);
RL();
} #region Helper methods private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
} private static void RL()
{
Console.ReadLine();
} private static void Break()
{
System.Diagnostics.Debugger.Break();
} #endregion
}
调试的时候显示
mi1.Invoke(MyA,null);
未处理的nullreferenceexception
未将对象引用设置到对象的实例
希望大家赐教
解决方案 »
- HQL高手看过来
- 散分,一个database的sqlserver封装类
- 如何新建Windows service项目
- access数据库,我想初始化表里面的数据怎么做啊?
- 如何自己从零开始做一个控件?
- 如何终止线程池里的线程?
- win7 64位系统下开发,到客户xp上能用吗?
- 为什么我把vb代码移植到c#上 运行很慢 很慢
- 在C#中使用Socket时,如何使用异步模式,就是可以获得网络消息
- .NET Framework主要由公共语言运行时(CLR)和基础类库(BCL)两部分组成。请问常用的类(比如Console类)是不是就在基础类库(BCL)里面?
- 怎样把Table中的数据做成OUTLOOK的程序界面?
- 关于DataSet 生成xml格式问题
using System;
using System.Reflection;
using System.Collections.Generic;
public class A
{
private void method() { Console.WriteLine("I am a private method in class A "); }
}public class B
{
public void method() { Console.WriteLine("I am a public method in class B "); }
}public class MyClass
{
public static void Main()
{
RL(); Console.WriteLine("\nReflection.MethodInfo\n ");
A MyA = new A();
B MyB = new B(); // Get the Type and MethodInfo.
Type t = typeof(A);
MethodInfo mi1 = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance); mi1.Invoke(MyA, null); Type tt = typeof(B);
MethodInfo mi2 = tt.GetMethod("method");
mi2.Invoke(MyB, null); RL();
} #region Helper methods private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
} private static void RL()
{
Console.ReadLine();
} private static void Break()
{
System.Diagnostics.Debugger.Break();
} #endregion
}
MethodInfo mi1 = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance);"method "
---->"method"
mi1.Invoke(MyA, null);改成:
t.InvokeMember("method", BindingFlags.InvokeMethod, System.Type.DefaultBinder, MyA, null);另外:Class A的method应为public
MethodInfo mi1 = MyA.GetType().GetMethod("method ");
mi1.Invoke(MyA, null);
MethodInfo mi2 = MyB.GetType().GetMethod("method");
mi2.Invoke(MyB, null);
感谢你的回复,不好意思啊,结贴太早!而且现在还不能加分。
不管怎样,多谢!大家共同进步!
mi1.Invoke(MyA, null); 改成:
t.InvokeMember("method", BindingFlags.InvokeMethod, System.Type.DefaultBinder, MyA, null); 另外:Class A的method应为public我这段代码是csdn另一个帖子里面的。
是一位网友写的。
有点疑问:Class A的method为private时程序也可以通过?希望tigerlgf赐教!