泛型列表 [英] lists of generics

查看:85
本文介绍了泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个接受类型参数的类。然后,我想将所有这些内容放入一个列表<> ;.


列表<参数< object>> lp = new List< Parameter< object>>();


参数< string> licenceKey = new参数< string>();

licenceKey.Name =" LICENSEKEY";

licenceKey.Section =" General";

licenceKey.Value =" IVS" ;; //默认3个用户密钥

lp.Add(licenceKey);


参数< bool> autoInsertDevice = new Parameter< bool>();

autoInsertDevice.Name =" AUTOINSERTDEVICE";

autoInsertDevice.Section =" General";

autoInsertDevice.Value = true;

lp.Add(licenceKey);


我想通过在列表对象中创建列表允许的类型

从要插入的对象派生的类型对象。我错了。

我该如何解决这个问题?

I created a class that accepts a type parameter. I then want to put all
of those into one list<>.

List<Parameter<object>> lp = new List<Parameter<object>>();

Parameter<string> licenceKey = new Parameter<string>();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

Parameter<bool> autoInsertDevice = new Parameter<bool>();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

I thought by making the type in the list object the list would allow
objects that are typed derived from object to be inserted. I was wrong.
How can i fix this?

推荐答案

http://www.boyet.com/Articles/CSharpCovarianceOne.html

http://www.boyet.com/Articles/CSharpCovarianceOne.html


泛型在编译时是强类型的。它们不像继承的

类那样工作。如果你创建了一个参数< string>,它是一个类型的类,并且

不是Generic类型,它根本不是一个类型,而更像是一个

模板。


-

HTH,


Kevin Spencer

微软MVP

专业Numbskull


提出意见的人是明智的。

与事实争吵的男人是傻子。


" Dan Holmes" <哒******* @ bigfoot.com>在消息中写道

新闻:eO ************** @ TK2MSFTNGP04.phx.gbl ...
Generics are strongly-typed at compile-time. They do not work like inherited
classes. If you create a Parameter<string>, it is a class of that type, and
not of a Generic type, which is not strictly a type at all, but more like a
Template.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:eO**************@TK2MSFTNGP04.phx.gbl...
我创建了一个类接受类型参数。然后我想把所有这些都放在一个列表中<> ;.

List< Parameter< object>> lp = new List< Parameter< object>>();

参数< string> licenceKey = new参数< string>();
licenceKey.Name =" LICENSEKEY";
licenceKey.Section =" General";
licenceKey.Value =" IVS" ;; //默认3个用户密钥
lp.Add(licenceKey);

参数< bool> autoInsertDevice = new Parameter< bool>();
autoInsertDevice.Name =" AUTOINSERTDEVICE";
autoInsertDevice.Section =" General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

我想通过在列表对象中创建类型,列表将允许插入从对象派生的类型对象。我错了。
我该如何解决这个问题?
I created a class that accepts a type parameter. I then want to put all of
those into one list<>.

List<Parameter<object>> lp = new List<Parameter<object>>();

Parameter<string> licenceKey = new Parameter<string>();
licenceKey.Name = "LICENSEKEY";
licenceKey.Section = "General";
licenceKey.Value = "IVS"; //default 3 user key
lp.Add(licenceKey);

Parameter<bool> autoInsertDevice = new Parameter<bool>();
autoInsertDevice.Name = "AUTOINSERTDEVICE";
autoInsertDevice.Section = "General";
autoInsertDevice.Value = true;
lp.Add(licenceKey);

I thought by making the type in the list object the list would allow
objects that are typed derived from object to be inserted. I was wrong.
How can i fix this?



sd ******** @ gmail.com 写道:
http://www.boyet.com/Articles/CSharpCovarianceOne.html



该文章超时。我确实读过协方差的内容,但我不知道这对我有多大帮助。


that article timed out. I did go read stuff on covariance but i don''t
understand how that helps me.


这篇关于泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆