关于类型作为泛型传入参数的问题

lyyiangang2 2012-04-10 10:33:15
现在想通过传入一个类型来实现泛型容器的初始化(例如Init(int),Init(float),Init(string)),但是怎么把Type转换为T呢?总是报错,请大家指教啊。下面是代码。
class Test
{
List<T> m_list;
void Init(Type tt)
{
m_list=new List<tt>();
}
}
...全文
594 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2012-04-11
  • 打赏
  • 举报
回复
你想要的class 不是泛型类么?如果是泛型类,还需要传入类型参数?



cheng2005 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

引用 14 楼 的回复:

那你把Inti方法做成泛型方法。
用Type来生成T这个想法就是不对的,泛型真不是这么玩的。

呵呵,我对泛型的使用也就仅限于list,dictionary,理解不深,请多多指教哈。
[/Quote]
首先你要明白,泛型不是动态类型。
泛型如果简要的说应该属于设计时概念,而不是运行时概念。
就是说泛型是在设计时确定的类型,而不是在运行时确定类型。
lyyiangang2 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

那你把Inti方法做成泛型方法。
用Type来生成T这个想法就是不对的,泛型真不是这么玩的。
[/Quote]
呵呵,我对泛型的使用也就仅限于list,dictionary,理解不深,请多多指教哈。
cheng2005 2012-04-11
  • 打赏
  • 举报
回复
那你把Inti方法做成泛型方法。
用Type来生成T这个想法就是不对的,泛型真不是这么玩的。
lyyiangang2 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

pulbic class BaseCombo<T>:System.Windows.Forms.ComboBox
{
List<T> m_list=new List<T>();//
}
这个没什么问题。
但是为什么要做这个呢?
pulbic class IntCombo<int>:BaseCombo<T>
{
}

用的时候直接BaseCombo<int> temp = ……
[/Quote]
这是个combox控件,我想先在窗体摆放这些控件,然后可以通过Inti(type )函数来具体确定这些控件中存储的数据类型,如果用BaseCombo<int> temp = new BaseCombo<int>()动态生成的话需要控制好控件的位置,另外写一些代码,比较麻烦了,谢谢回复哈
孙大诚_SunRobin 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
……
[/Quote]


+1
cheng2005 2012-04-11
  • 打赏
  • 举报
回复
pulbic class BaseCombo<T>:System.Windows.Forms.ComboBox
{
List<T> m_list=new List<T>();//
}
这个没什么问题。
但是为什么要做这个呢?
pulbic class IntCombo<int>:BaseCombo<T>
{
}

用的时候直接BaseCombo<int> temp = new BaseCombo<int>();
为什么还要继承一次呢?范性不是你那么用的。
lyyiangang2 2012-04-11
  • 打赏
  • 举报
回复
好吧,估计这个问题我太简化了,我说下这个问题的来源吧:
pulbic class BaseCombo<T>:System.Windows.Forms.ComboBox
{
List<T> m_list=new List<T>();//
}
现在我们来继承这个BaseCombo,
pulbic class IntCombo<int>:BaseCombo<T>
{
}
要是string类型的combbox
pulbic class IntCombo<string>:BaseCombo<T>
{
}
但是实际上要用的数据类型还有很多,总不能每次用都要继承一次吧,太繁琐,我想做的是能不能定义一个下面类似的类:
pulbic class BaseCombo:System.Windows.Forms.ComboBox
{
List<T> m_list;//
void Init(Type tt)
{
m_list=new List<tt>();//具体的类型
}
}
所以我才会在上面问道哪个简化过的问题,还要请高手们指点啊
stonespace 2012-04-11
  • 打赏
  • 举报
回复
生成和泛型参数或者方法参数的实例,只能通过反射,就如同4楼这样:

_list = (List<T>)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(T)));

其实效率很低,
cheng2005 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

那这个问题不就无解了么。
[/Quote]
你就是在运行时根据Type去创建集合。
4楼的方法是可行的,不过,如非必要,本人不推荐。
事理 2012-04-11
  • 打赏
  • 举报
回复
看看这个
//根据对象类型克隆对象
ConstructorInfo[] ci = typeof(tt).GetConstructors();
object objClone = ci[0].Invoke(new object[0]);
cheng2005 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

那这个问题不就无解了么。
[/Quote]
你现在的问题是,你想在运行时取到一个Type,然后根据这个Type生成一个集合。
4楼的方法是可行的,但是如果不是必要,本人不推荐。
lyyiangang2 2012-04-11
  • 打赏
  • 举报
回复
那这个问题不就无解了么。
lyyiangang2 2012-04-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 5 楼 的回复:

恩,直接 new List<T>(); 就可以了嘛。。哈哈

这样不好啊,m_list中存入的数据类型不一定是什么,只是想当需要时动态生成而已,比如需要int型的就用Init(Type int),需要string就用Intit(Type String),这样能满足不同时期的需要。谢谢指教了哈
[/Quote]
其实也就是想知道怎么把外部的Type转换为泛型的T
lyyiangang2 2012-04-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

恩,直接 new List<T>(); 就可以了嘛。。哈哈
[/Quote]
这样不好啊,m_list中存入的数据类型不一定是什么,只是想当需要时动态生成而已,比如需要int型的就用Init(Type int),需要string就用Intit(Type String),这样能满足不同时期的需要。谢谢指教了哈
机器人 2012-04-10
  • 打赏
  • 举报
回复
恩,直接 new List<T>(); 就可以了嘛。。哈哈
机器人 2012-04-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Test<int> test = new Test<int>();
test.Init();
test.Add(1);
Console.WriteLine(test[0]);
Console.Read();
}
}

class Test<T>
{
private List<T> _list;
public void Init()
{
_list = (List<T>)Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(T)));

}
public void Add(T t)
{
_list.Add(t);
}
public T this[int i]
{
get { return _list[i]; }
}

}
}
JM 2012-04-10
  • 打赏
  • 举报
回复
class Test<T>
{
public List<T> m_list=new List<T>();
}
}


使用方法:
Test<int> test = new Test<int>();
test.m_list.Add(1);

lyyiangang2 2012-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

不明白什么意思?Default(T) 不能满足你的要求?
[/Quote]
抱歉,没有写清楚
void Init(Type tt)
{
m_list=new List<tt>();//这段出现编译错误
}
我是想将类型当做值传入到init中,从而根据传入的tt类型实例化出不同类型的m_list
机器人 2012-04-10
  • 打赏
  • 举报
回复
不明白什么意思?Default(T) 不能满足你的要求?

110,533

社区成员

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

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

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