例如:Form frm = new Form();
判断 Form 是否有load()方法
就是我new一个对象之后,判断这个对象是否具有load()方法
判断 Form 是否有load()方法
就是我new一个对象之后,判断这个对象是否具有load()方法
解决方案 »
- DevExpress报表的SecondaryAxisX问题
- c#调用c++ dll问题
- 看看这个问题,关于查找控件的问题
- 一个关于foreach循环的小问题
- C# 怎么实现这个功能?
- 请教一段ADO.NET的代码
- 求一控件:类似DataGridView,但可以控制每个单元格,而不是列
- 我做的web有个很怪的问题,大家帮帮我吧
- 请教使用config文件的工程的调试方法
- 天啦,我又完蛋了,我写的com+组件不知道怎么回事,有时程序连接com+组件会失败,我重新编译它都不行,然后我重新注册一下,就可以用了,
- 公司要炒我了啊@@@@@@@还不做完@@@@@@@@@@@@@@短信如何实时接收?????????求助大家了啊!!!!!!!!!!!!!!!!
- C# 怎样做多个字符的替换?TextBox1.Text.Replace的问题。
利用反射去处理
不过这种情况容易有误差.如果想判断的话,在代码里面写"frm.load()",然后编译,看是否出错咯.不知会不会理解错楼主的原意?
public class Class1
{
public Class1( )
{
} public string Method1( )
{
return " word";
} public string Method2( string strName )
{
return strName;
} }
public class Class2
{
public Class2( )
{
} public bool FindMethod( string className , string methodName , Type[ ] paramType)
{
Type type = Type.GetType( className , true , true );
object dObj = Activator.CreateInstance( type );
MethodInfo method;
if( paramType!= null )
method = type.GetMethod( methodName , paramType);
else
method = type.GetMethod( methodName , new Type[0] ); return method != null;
}
}//调用:
Response.Write( new Class2( ).FindMethod( "Class1" , "Method1" , null );
Response.Write( new Class2( ).FindMethod( "Class1" , "Method2" , new Type[ ] { typeof( string ) } ) );
楼主在MSDN里找Reflection的资料看看
其他的方法基本上都得到了;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
if (i.Name.ToLower().IndexOf("load") > -1)
{
MessageBox.Show(i.Name);
}
} } }
}
{
Form frm = new Form();
frm.Load();
Response.write("有此方法");
}
catch
{
Response.write("无此方法");
}