关于C#的类型问题 如果传入一字符串,例如传入"mycontrol",mycontrol又是一class,但是我想用这个类,如何实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是说给方法传入类?public void 方法名(类名 mycontrol){ //} Type t = System.Type.GetType("System.String")可以得到类型 我是使用usercontrol,给某aspx传入一个ascx控件,因为传入的只是ascx的名字,即字符串名,但我想使用控件中的类,该如何实现 protected void Page_Load(object sender, EventArgs e) { string controlName = Request.QueryString["uc"]; Type MyControl = System.Type.GetType(controlName); UserControl control = (UserControl)LoadControl(controlName+".ascx"); ph.Controls.Add(control); //strSql = (control as mycontrol).getSqlString(); Response.Write(strSql); }这是调用的代码,controlName就是传入的字符串,注释的就是我想实现的 可以用反射来做, /// <summary> /// 执行指定的方法 /// </summary> /// <param name="FilePath">方法所在的文件路径</param> /// <param name="ClassName">方法所在的命名空间及类名称</param> /// <param name="FunctionName">方法名称</param> /// <param name="Para">参数集合</param> public static void RunReflection(string FilePath, string ClassName, string FunctionName, object[] Para) { try { System.Reflection.Assembly A = Assembly.LoadFrom(FilePath); Type t = A.GetType(ClassName); Console.WriteLine(ClassName); object obj = Assembly.GetAssembly(t).CreateInstance(ClassName);//创建实例 MethodInfo method = t.GetMethod(FunctionName);//提取方法信息 method.Invoke(obj, Para);//调用方法 } catch(Exception e) { throw new Exception("执行失败!" + "\n" + e.Message); } } System.Reflection.Assembly A = Assembly.LoadFrom(FilePath);这里的lord是指从dll中lord吧,我的文件是用户自定义控件,xxx.ascx文件好像行不通 对,是从dll里调的,如果这样不行不知道用工厂类做行不行,参考这个:http://www.jdon.com/designpatterns/designpattern_factory.htm Dev LookUpEdit 的苦恼 C#连接dbf数据库,需要下载安装个什么驱动程序? debug为什么不能用 用xmltextreader读取xml的简单问题 反射?? 遍历自定义类中全部属性 winform中,如何把datagrid中的数据插入到数据库中? 下面的程序为什么接收不到UDP数据? c# winform 水晶报表 C#中 如何把excel的数据每一一列的值,赋值给一个一位数组呢 VS2010 C#读取数据库中语乱码 问几个关于c/s下datagrid的几个问题 哪里有智能客户端的书吗?
public void 方法名(类名 mycontrol)
{
//
}
{
string controlName = Request.QueryString["uc"];
Type MyControl = System.Type.GetType(controlName);
UserControl control = (UserControl)LoadControl(controlName+".ascx");
ph.Controls.Add(control);
//strSql = (control as mycontrol).getSqlString();
Response.Write(strSql); }
这是调用的代码,controlName就是传入的字符串,注释的就是我想实现的
/// <summary>
/// 执行指定的方法
/// </summary>
/// <param name="FilePath">方法所在的文件路径</param>
/// <param name="ClassName">方法所在的命名空间及类名称</param>
/// <param name="FunctionName">方法名称</param>
/// <param name="Para">参数集合</param>
public static void RunReflection(string FilePath, string ClassName, string FunctionName, object[] Para)
{
try
{
System.Reflection.Assembly A = Assembly.LoadFrom(FilePath);
Type t = A.GetType(ClassName);
Console.WriteLine(ClassName);
object obj = Assembly.GetAssembly(t).CreateInstance(ClassName);//创建实例 MethodInfo method = t.GetMethod(FunctionName);//提取方法信息
method.Invoke(obj, Para);//调用方法
}
catch(Exception e)
{
throw new Exception("执行失败!" + "\n" + e.Message);
}
}
这里的lord是指从dll中lord吧,我的文件是用户自定义控件,xxx.ascx文件
好像行不通