CAB,泛型与typeof,编译出错,请高手指教!

wolfpzp 2006-12-19 01:47:59
使用Composit UI,有如下三段代码:
1、WorkItem类
public class EntityItem<TEntity> : WorkItem
2、View类
public class TView : UserControl
3、调用视图的方法
public void onshow()
{
通过一个类名反射取得类型
Type type = Type.GetType("ERP.Server.Order,ERP.Server");

使用泛型的workitem显示视图
EntityItem<type> workitem = WorkItem.Items.AddNew<EntityItem<type>>();

///////////////////////////////
///////////显示窗体////////////
///////////////////////////////
}

现在编译就提示错误:
The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)

请问:
1、是否因为泛型要求类型安全引起?
2、用这种方式是否可行?
3、是否有其他更好的方式?
...全文
241 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
www_123du_com 2006-12-21
  • 打赏
  • 举报
回复
呵呵,泛型的好处就是可以在编译时获取其类型。

没看懂你要做什么,麻烦你详细描述一下目的。
zhaochong12 2006-12-21
  • 打赏
  • 举报
回复
http://www.codeproject.com/csharp/invokebencharking.asp
mobydick 2006-12-21
  • 打赏
  • 举报
回复
EntityItem<type> workitem = WorkItem.Items.AddNew<EntityItem<type>>();这句
改为
EntityItem<type> workitem = WorkItem.Items.AddNew<type>();
试试。
bigrongshu 2006-12-19
  • 打赏
  • 举报
回复
泛型里不能传入变量,

private void test()
{
//Type t = typeof(Form1);
Build<Form1>(this);
}

private void Build<T>(T form)
where T: Control
{
form.Show();
}
wolfpzp 2006-12-19
  • 打赏
  • 举报
回复
那如何可以获得该类型呢?
因为系统很多地方都用的是泛型,这就要求需要知道类型。
而每次show都不一定一样,通过传参数告诉需要显示哪个view。
请指教。
谢谢!
babyrockxray 2006-12-19
  • 打赏
  • 举报
回复
泛型<>接受的是类型吧, type是Type类型的对象,你这么使用应该不对
可以把type作为参数传进去,然后动态生成实例

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧