仿制药收集问题 [英] generics collection question

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

问题描述

你好,


我希望用Generics做点什么,但我不确定它是否可能。

让据说我希望有一堆业务对象和一个数据访问类

与每个业务对象相关。对于每个业务对象,我会

如下所示:


公共类Apple

{

//苹果的东西

}


公共类AppleDAL

{

public void UpdateApple (int appleID,Apple apple);

public Apple GetApple(int appleID);

public AppleList ListAllApples();

}


目前,AppleList集合类是CollectionBase的子类。

每当我添加一个新的业务对象时,我需要编写一个新的DAL类和一个

新的CollectionBase子类充当该业务的集合

对象:


///< summary>

/// Apple对象的类型安全集合

///< / summary>

[可序列化]

公共类AppleList:System.Collections.CollectionBase

{

public AppleList {}

public Apple this [int index]

{

get {return(Apple List [index]); }

set {List [index] = value; }

}


public int Add(Apple item){return(List.Add(item)); } $ / $

public int IndexOf(Apple item){return(List.IndexOf(item)); } $ / $

public void Insert(int index,Apple item){List.Insert(index,

item); }


public void删除(Apple项目){List.Remove(item); }

}

我想知道是否有办法使用Generics来避免为每个新业务对象编写

CollectionBase子类。我不确定我将如何为新事物创建具体的类型安全集合。所以对于一个

的新业务对象,橙色:


公共类橙色

{

/ /橙色东西

}


公共类OrangeDAL

{

public void UpdateOrange(int orangeID ,橙色橙色);

public Orange GetOrange(int orangeID);

public OrangeList ListAllOranges();

}


我可以使用Generics创建一个OrangeList类吗?类似的东西?


//这不起作用

公共类OrangeList< Orange> {}


-Corey


---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----

解决方案

哎呀,科里!这很简单:


使用System.Collections.ObjectModel;


公共类AppleList:Collection< Apple {}


这就是你所需要的。它已经有了你必须连接的方法

,比如添加,删除,IndexOf等。


-

HTH,


Kevin Spencer

微软MVP

鸡肉沙拉手术


谁是Mighty Abbott?双炮塔scalawag。


" cwineman" < me@home.com写了留言

新闻:11 ************** @ sp6iad.superfeed.net ...


你好,


我希望用Generics做点什么,但我不确定它是否可能。

假设我想拥有一堆业务对象和数据访问

类与每个业务对象相关。对于每个业务对象,我将获得如下内容:


公共类Apple

{

//苹果的东西

}


公共类AppleDAL

{

public void UpdateApple (int appleID,Apple apple);

public Apple GetApple(int appleID);

public AppleList ListAllApples();

}


目前,AppleList集合类是CollectionBase的子类。

每当我添加一个新的业务对象时,我需要编写一个新的DAL类并且

a新的CollectionBase子类充当该业务的集合

对象:


///< summary>

/// Apple对象的类型安全集合

///< / summary>

[可序列化]

公共类AppleList :System.Collections.CollectionBase

{

public AppleList {}


public Apple this [int index]

{

get {return(Apple List [index]); }

set {List [index] = value; }

}


public int Add(Apple item){return(List.Add(item)); } $ / $

public int IndexOf(Apple item){return(List.IndexOf(item)); } $ / $

public void Insert(int index,Apple item){List.Insert(index,

item); }


public void删除(Apple项目){List.Remove(item); }

}


我想知道是否有办法使用泛型来避免为每个新的编写

CollectionBase子类业务对象。我不确定我将如何为新事物创建具体的类型安全集合。所以对于一个

的新业务对象,橙色:


公共类橙色

{

/ /橙色东西

}


公共类OrangeDAL

{

public void UpdateOrange(int orangeID ,橙色橙色);

public Orange GetOrange(int orangeID);

public OrangeList ListAllOranges();

}


我可以使用Generics创建一个OrangeList类吗?类似的东西?


//这不起作用

公共类OrangeList< Orange> {}


-Corey


---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制安全Usenet

新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000 +

新闻组

---- = East和West-Coast服务器场 - 通过加密实现全面隐私

= ----



当然,定义一个基础DataCollection和DataRecord对象。然后创建

您的基本DAL实体类和集合,然后您可以创建

自定义类(我说这是因为我猜你的
DAL可能是自动生成的。


对于DAL这样的事情:


公共抽象类OrangeCollectionBase< T :DataCollection< Twhere

T:OrangeBase,new()

{

}


然后你派生的集合类看起来像这样:


public class Oranges:OrangeCollectionBase< Orange>

{

}


cwineman写道:


您好,


我希望能做点什么使用Generics,但我不确定它是否可能。

假设我想拥有一堆业务对象和一个数据访问类

协调每个业务对象。对于每个业务对象,我会

如下所示:


公共类Apple

{

//苹果的东西

}


公共类AppleDAL

{

public void UpdateApple (int appleID,Apple apple);

public Apple GetApple(int appleID);

public AppleList ListAllApples();

}


目前,AppleList集合类是CollectionBase的子类。

每当我添加一个新的业务对象时,我需要编写一个新的DAL类和一个

新的CollectionBase子类充当该业务的集合

对象:


///< summary>

/// Apple对象的类型安全集合

///< / summary>

[可序列化]

公共类AppleList:System.Collections.CollectionBase

{

public AppleList {}

public Apple this [int index]

{

get {return(Apple List [index]); }

set {List [index] = value; }

}


public int Add(Apple item){return(List.Add(item)); } $ / $

public int IndexOf(Apple item){return(List.IndexOf(item)); } $ / $

public void Insert(int index,Apple item){List.Insert(index,

item); }


public void删除(Apple项目){List.Remove(item); }

}


我想知道是否有办法使用泛型来避免为每个新的编写

CollectionBase子类业务对象。我不确定我将如何为新事物创建具体的类型安全集合。所以对于一个

的新业务对象,橙色:


公共类橙色

{

/ /橙色东西

}


公共类OrangeDAL

{

public void UpdateOrange(int orangeID ,橙色橙色);

public Orange GetOrange(int orangeID);

public OrangeList ListAllOranges();

}


我可以使用Generics创建一个OrangeList类吗?类似的东西?


//这不起作用

公共类OrangeList< Orange> {}


-Corey


---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东和西海岸服务器农场 - 通过加密的总隐私= ----


< blockquote>很好。


我相当肯定它可以轻松完成,但我无法弄明白

的语法,我不是''我能找到一个显示我想要的例子。


谢谢,伙计。


" Kevin Spencer" < uc *@ftc.govwrote in message

news:eQ ****************** @ TK2MSFTNGP04.phx.gbl ...


哎呀,Corey!这很简单:


使用System.Collections.ObjectModel;


公共类AppleList:Collection< Apple {}


这就是你所需要的。它已经有了你必须连接的方法

,比如添加,删除,IndexOf等。


-

HTH,


Kevin Spencer

微软MVP

鸡肉沙拉手术


谁是Mighty Abbott?双炮塔scalawag。




---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全Usenet新闻== ----
< a rel =nofollowhref =http://www.newsfeeds.comtarget =_ blank> http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----


Hello,

I''m hoping to do something using Generics, but I''m not sure it''s possible.
Let''s say I want to have a bunch of business objects and a data access class
cooresponding to each business object. For each business object, I would
have something like below:

public class Apple
{
//apple stuff
}

public class AppleDAL
{
public void UpdateApple( int appleID, Apple apple );
public Apple GetApple( int appleID );
public AppleList ListAllApples();
}

Currently, the AppleList collection class is a subclass of CollectionBase.
Everytime I add a new business object, I need to write a new DAL class and a
new CollectionBase subclass to act as the collection for that business
object:

/// <summary>
/// Type-safe collection for Apple objects
/// </summary>
[Serializable]
public class AppleList : System.Collections.CollectionBase
{
public AppleList {}

public Apple this[ int index ]
{
get{ return( Apple List[index] ); }
set{ List[index] = value; }
}

public int Add( Apple item ) { return( List.Add( item ) ); }

public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }

public void Insert( int index, Apple item ) { List.Insert( index,
item ); }

public void Remove( Apple item ) { List.Remove( item ); }
}
I am wondering if there is a way to use Generics to avoid writing the
CollectionBase subclass for every new business object. I''m not sure how I
would create a concrete type-safe collection for something new. So for an
new business object, orange:

public class Orange
{
//orange stuff
}

public class OrangeDAL
{
public void UpdateOrange( int orangeID, Orange orange );
public Orange GetOrange( int orangeID );
public OrangeList ListAllOranges();
}

Can I create an OrangeList class using Generics? Something like?

//this doesn''t work
public class OrangeList <Orange>{}

-Corey

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

解决方案

Heck yes, Corey! It''s quite simple:

using System.Collections.ObjectModel;

public class AppleList : Collection<Apple{}

That''s all you need. It already has the methods that you had to wire up
before, like Add, Remove, IndexOf, etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.

"cwineman" <me@home.comwrote in message
news:11**************@sp6iad.superfeed.net...

Hello,

I''m hoping to do something using Generics, but I''m not sure it''s possible.
Let''s say I want to have a bunch of business objects and a data access
class cooresponding to each business object. For each business object, I
would have something like below:

public class Apple
{
//apple stuff
}

public class AppleDAL
{
public void UpdateApple( int appleID, Apple apple );
public Apple GetApple( int appleID );
public AppleList ListAllApples();
}

Currently, the AppleList collection class is a subclass of CollectionBase.
Everytime I add a new business object, I need to write a new DAL class and
a new CollectionBase subclass to act as the collection for that business
object:

/// <summary>
/// Type-safe collection for Apple objects
/// </summary>
[Serializable]
public class AppleList : System.Collections.CollectionBase
{
public AppleList {}

public Apple this[ int index ]
{
get{ return( Apple List[index] ); }
set{ List[index] = value; }
}

public int Add( Apple item ) { return( List.Add( item ) ); }

public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }

public void Insert( int index, Apple item ) { List.Insert( index,
item ); }

public void Remove( Apple item ) { List.Remove( item ); }
}
I am wondering if there is a way to use Generics to avoid writing the
CollectionBase subclass for every new business object. I''m not sure how I
would create a concrete type-safe collection for something new. So for an
new business object, orange:

public class Orange
{
//orange stuff
}

public class OrangeDAL
{
public void UpdateOrange( int orangeID, Orange orange );
public Orange GetOrange( int orangeID );
public OrangeList ListAllOranges();
}

Can I create an OrangeList class using Generics? Something like?

//this doesn''t work
public class OrangeList <Orange>{}

-Corey
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----



Sure, define a base DataCollection and DataRecord object. Then create
your base DAL entity class and collection and then you can create
custom classes on top of those (I say this because I''m guessing your
DAL is probably auto-generated).

Something like this for the DAL:

public abstract class OrangeCollectionBase<T: DataCollection<Twhere
T : OrangeBase, new()
{
}

Then your derived collection class would look like this:

public class Oranges : OrangeCollectionBase<Orange>
{
}

cwineman wrote:

Hello,

I''m hoping to do something using Generics, but I''m not sure it''s possible.
Let''s say I want to have a bunch of business objects and a data access class
cooresponding to each business object. For each business object, I would
have something like below:

public class Apple
{
//apple stuff
}

public class AppleDAL
{
public void UpdateApple( int appleID, Apple apple );
public Apple GetApple( int appleID );
public AppleList ListAllApples();
}

Currently, the AppleList collection class is a subclass of CollectionBase.
Everytime I add a new business object, I need to write a new DAL class and a
new CollectionBase subclass to act as the collection for that business
object:

/// <summary>
/// Type-safe collection for Apple objects
/// </summary>
[Serializable]
public class AppleList : System.Collections.CollectionBase
{
public AppleList {}

public Apple this[ int index ]
{
get{ return( Apple List[index] ); }
set{ List[index] = value; }
}

public int Add( Apple item ) { return( List.Add( item ) ); }

public int IndexOf( Apple item ) { return( List.IndexOf( item ) ); }

public void Insert( int index, Apple item ) { List.Insert( index,
item ); }

public void Remove( Apple item ) { List.Remove( item ); }
}
I am wondering if there is a way to use Generics to avoid writing the
CollectionBase subclass for every new business object. I''m not sure how I
would create a concrete type-safe collection for something new. So for an
new business object, orange:

public class Orange
{
//orange stuff
}

public class OrangeDAL
{
public void UpdateOrange( int orangeID, Orange orange );
public Orange GetOrange( int orangeID );
public OrangeList ListAllOranges();
}

Can I create an OrangeList class using Generics? Something like?

//this doesn''t work
public class OrangeList <Orange>{}

-Corey

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Excellent.

I was fairly certain that it could be easily done, but I couldn''t figure out
the syntax and I wasn''t able to find an example showing what I wanted.

Thanks, guys.

"Kevin Spencer" <uc*@ftc.govwrote in message
news:eQ******************@TK2MSFTNGP04.phx.gbl...

Heck yes, Corey! It''s quite simple:

using System.Collections.ObjectModel;

public class AppleList : Collection<Apple{}

That''s all you need. It already has the methods that you had to wire up
before, like Add, Remove, IndexOf, etc.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于仿制药收集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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