泛型列表< myObject< T>>,这可能与Tpossibly更改? [英] List of generics List<myObject<T>>, is this possible with the Tpossibly changing?
问题描述
大家好,
我有一个类(以示例myObject命名)可以是几个类型的元数(int,string,float,等)而不是使用一个对象
定义它的类型我使用了泛型。
公共类MyObject< T:ChangeObject
{
...
public MyObject(字符串名称,T值)
{
//调用属性,因为还有额外的工作
做
名称=名称;
值=值;
}
}
到现在为止,一切正常,我现在要做的是设置一个
属性是一个类,它是这些对象的列表。
所以我认为它很容易:
列表< MyObject< T>对象;
甚至:
MyObject< T> []对象;
>
但是看起来T只适用于类定义,这是
是真的吗?
我该怎么办? ave这类通用对象的列表?
谢谢
T是为声明它的泛型类型或泛型
方法定义的。因此,MyObject< Tcan中的任何代码都可以使用
T.但是,MyObject之外的代码< Thas不知道*你是哪个* T
谈论的。
在常规的C#代码中,你必须说明你在说什么 -
,例如你可能有一个List< MyObject< Foo>对象。
或者,如果你在泛型类型/方法中,你可以使用你的
本地泛型参数:
public void Bar< TSomethingElse>(){
List< MyObject< TSomethingElse> objects = ...
}
(TSomethingElse here命名以区分这是一个*不同的*
MyObject< T>中的T的泛型类型参数。)
约束和类型推断可以大大简化了这一点 - 但是它很难显示一个有意义的例子而没有更多关于
你想要做什么的背景。
Marc
T是为声明它的泛型类型或泛型
方法定义的。因此,MyObject< Tcan中的任何代码都可以使用
T.但是,MyObject之外的代码< Thas不知道*你是哪个* T
谈论的。
在常规的C#代码中,你必须说明你在说什么 -
,例如你可能有一个List< MyObject< Foo>对象。
或者,如果你在泛型类型/方法中,你可以使用你的
本地泛型参数:
public void Bar< TSomethingElse>(){
List< MyObject< TSomethingElse> objects = ...
}
(TSomethingElse这里命名为区分这是一个*不同的*
泛型类型参数到MyObject中的T< T>)。
约束和类型推断可以大大简化这一点 - 但是如果没有更多关于
你想要做什么的更多背景,很难显示一个有意义的例子。
Marc
6月19日上午10点20分,Marc Gravell< marc.grav ... @ gmail.comwrote:
T是为声明它的泛型类型或泛型
方法定义的。因此,MyObject< Tcan中的任何代码都可以使用
T.但是,MyObject之外的代码< Thas不知道*你是哪个* T
谈论的。
在常规的C#代码中,你必须说明你在说什么 -
,例如你可能有一个List< MyObject< Foo>对象。
或者,如果你在泛型类型/方法中,你可以使用你的
本地泛型参数:
public void Bar< TSomethingElse>(){
List< MyObject< TSomethingElse> objects = ...
}
(TSomethingElse这里命名为区分这是一个*不同的*
泛型类型参数到MyObject中的T< T>)。
约束和类型推断可以大大简化这一点 - 但是如果没有更多关于
你想要做什么的更多背景,很难显示一个有意义的例子。
Marc
嗨Marc,
感谢您的回复。
我会尽力给你一个具体的例子:
我们有几个对象可以有一组用户属性
(由用户定义),这些属性可以是几种类型(int,
string ,浮动等),因此,我们定义了一个UserDefinedAttribute类:
公共类UserDefinedAttribute< T>
{
[ XmlElement(" Value")]
公共T值
{
得到{
返回this.value ;
}
设置{
//确保不违反类型信息
validateType(value);
this.value = value;
//如果未知则推断类型和大小信息
inferType();
}
}
private void inferType()
{
if(this.dataType ==(sbyte)Globals.DataTypesEnum.Unknown)
{
Type type = typ eof(T);
if(type.IsArray)
{
this.size =(this.value as Array).Length;
}
if(type == typeof(int)|| type == typeof(int []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Integer;
}
else if(type == typeof(bool)|| type ==
typeof(bool []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Boolean;
}
else if(type == typeof(string)|| type ==
typeof(string []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.String;
}
else if(type == typeof(float)|| type ==
typeof(float []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum .Float;
}
else
{
}
}
}
public UserDefinedAttribute(字符串名称,T值)
{
//调用属性,因为有额外的工作
todo
姓名=姓名;
价值=价值;
}
}
所以,属性已定义。
现在我有其他类如Auto必须有一个列表
UserDefinedAttribute,原始开发人员使用
这个,但我希望有类似
List< UserDefinedAttribute< T>>,但框架不允许我。
我知道我可以拥有List< UserDefinedAttribute< string>或
List< UserDefinedAttribute< int>>,但由于类型可能不同于
UserDefinedAttribute到UserDefinedAttribute I想要一个
通用类型。
你能指出我正确的方向吗?
谢谢
对;在这种情况下,我可能会使用非通用接口 -
soemthing如下面的IUserDefinedType。然后你可以得到一个
IUserDefinedType的列表。如果T可以在列表中的每个项目更改
,那么这可能是最接近的。
另请注意,还有其他方法可以对灵活属性进行建模 -
例如,如果使用数据绑定,System.ComponentModel允许您通过ICustomTypeDescriptor或
TypeDescriptionProvider获得运行时属性。相当大的参与区域,但如果您使用
数据绑定,请告诉我 - 我可以给你一些提示让这个工作......
Marc
公共接口IUserDefinedType
{
object Value {get;组; }
类型类型{get; } $ / $
}
公共接口IUserDefinedType< T:IUserDefinedType
{
new T Value {get;组; }
}
公共类UserDefinedType< T:IUserDefinedType< T>
{
public T Value {get ;组; } $ / $
public Type Type {get {return typeof(T); }} //等等
对象IUserDefinedType.Value
{
get {return Value; }
设置{Value =(T)value; }
}
}
Hi all,
I have a class (named for the example myObject) that can be of
several types (int, string, float, etc), instead of using a object to
define it''s type I used a generic.
public class MyObject<T: ChangeObject
{
...
public MyObject(string name, T value)
{
// invoke the properties, since there''s additional work to
do
Name = name;
Value = value;
}
}
Until now, everything is ok, what I want to do now, is to set a
property inside a class that is a list of these objects.
So I thought it would be has easy has:
List<MyObject<T>Objects;
or even:
MyObject<T>[] Objects;
But it seems that the T only works on class definitions, is this
true?
What can I do to have a list of generic objects of this kind?
Thanks
The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.
In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:
public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...
}
(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).
Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.
Marc
On Jun 19, 10:20 am, Marc Gravell <marc.grav...@gmail.comwrote:The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.
In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:
public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...
}
(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).
Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.
MarcOn Jun 19, 10:20 am, Marc Gravell <marc.grav...@gmail.comwrote:
The "T" is defined for the entirety of the generic type or generic
method that declares it. So any code inside MyObject<Tcan make use of
T. However, code outside of MyObject<Thas no idea *which* T you are
talking about.
In your regular C# code, you must indicate what you are talking about -
for example you might have a "List<MyObject<Foo>objects".
Alternatively, if you are inside a generic type/method, you can use your
local generic arguments instead:
public void Bar<TSomethingElse>() {
List<MyObject<TSomethingElse>objects = ...
}
(TSomethingElse here named to distinguish that this is a *different*
generic type-argument to the T in MyObject<T>).
Constraints and type-inference can greatly simplify this - but it is
hard to show a meaningful example without more context as to what
exactly you want to do.
MarcHi Marc,
thanks for your reply.
I will try to give you my concrete example:
We have several objects that can have a set of user attributes
(defined by the user), these attributes can be of several types (int,
string, float, etc), so, we defined a UserDefinedAttribute class:
public class UserDefinedAttribute<T>
{
[XmlElement("Value")]
public T Value
{
get {
return this.value;
}
set {
// Make sure the type information is not violated
validateType(value);
this.value = value;
// Infer type and size information if unknown
inferType();
}
}
private void inferType()
{
if (this.dataType == (sbyte)Globals.DataTypesEnum.Unknown)
{
Type type = typeof(T);
if (type.IsArray)
{
this.size = (this.value as Array).Length;
}
if (type == typeof(int) || type == typeof(int []))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Integer;
}
else if (type == typeof(bool) || type ==
typeof(bool[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Boolean;
}
else if (type == typeof(string) || type ==
typeof(string[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.String;
}
else if (type == typeof(float) || type ==
typeof(float[]))
{
this.dataType =
(sbyte)Globals.DataTypesEnum.Float;
}
else
{
}
}
}
public UserDefinedAttribute(string name, T value)
{
// invoke the properties, since there''s additional work
todo
Name = name;
Value = value;
}
}
So, the attributes are defined.
Now I have other classes like Auto that must have a list of
UserDefinedAttribute, the original developer used an array list for
this, but I would like to have something like
List<UserDefinedAttribute<T>>, but the framework doesn''t let me.
I know that I can have List<UserDefinedAttribute<string>or
List<UserDefinedAttribute<int>>, but since the type may vary from
UserDefinedAttribute to UserDefinedAttribute I would like to have a
generic type.
Can you point me in the right direction?
Thanks
Right; in this case, I might have a non-generic interface to use -
soemthing like IUserDefinedType below. Then you can have a list of
IUserDefinedType. That is the probably closest you can get if the T can
change per item in the list.
Note also that there are other ways of modelling flexible properties -
for example, if you use data-binding, System.ComponentModel allows you
to have runtime properties via ICustomTypeDescriptor or
TypeDescriptionProvider. Quite an involved area, but if you make use of
data-binding, let me know - I can give you some hints to get this working...
Marc
public interface IUserDefinedType
{
object Value { get; set; }
Type Type { get; }
}
public interface IUserDefinedType<T: IUserDefinedType
{
new T Value { get; set; }
}
public class UserDefinedType<T: IUserDefinedType<T>
{
public T Value { get; set; }
public Type Type { get { return typeof(T); } } // or whatever
object IUserDefinedType.Value
{
get { return Value; }
set { Value = (T)value; }
}
}
这篇关于泛型列表< myObject< T>>,这可能与Tpossibly更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!