扩展作为泛型返回的对象 [英] Extending objects that get returned as generics

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

问题描述

今天我遇到了一个独特的情况,我有一个核心库,

使用泛型从Active Directory返回用户。示例:


列表< ADUser> users = ADUser.GetByName(" First"," Last");


这很棒。但是,我需要做的是扩展ADUser

对象,如下所示:

class DataGridUser:ADUser

{

//添加一些新属性

//添加一些新的数据绑定方法

}


我因为GetByName()返回

a List< ADUser>,所以无法弄清楚如何扩展它。尝试这个不起作用:


List< DataGridUser> users = DataGridUser.GetByName(" First"," Last");


这会引发一个异常,即GetByName无法将DataGridUser转换为

ADUser。我错过了什么或做错了什么?有没有人有任何想法?


-Keith

解决方案

Keith Elder写道:

我今天遇到了一个独特的情况,我有一个核心库,
使用泛型从Active Directory返回用户。示例:

列表< ADUser> users = ADUser.GetByName(" First"," Last");

这很有用。但是,我需要做的是像这样扩展ADUser
对象:

类DataGridUser:ADUser
{
//添加一些新属性
//添加一些新的数据绑定方法


我不知道如何扩展它,因为GetByName()返回了一个List< ADUser>。尝试这个不起作用:

List< DataGridUser> users = DataGridUser.GetByName(" First"," Last");

这会引发一个异常,即GetByName无法将DataGridUser转换为ADUser。我错过了什么或做错了什么?有没有人有任何想法?




你确定编译器没有抱怨它无法转换

List< ADUser>列出< DataGridUser>?问题是List< ADUser>

和List< DataGridUser>完全不同的类型,即使

DataGridUser派生自ADUser。


这个新闻组最近有关于它的帖子,称为Casting />
泛型系列&继承" ;.如果

你在当地没有它,请查看groups.google.com吧。


如果这不是你的意思,请提供一个简短但完整的

示例来说明您的问题。请参阅
http://www.pobox.com/ ~sibet / csharp / complete.html 了解更多详情

我的意思。


Jon


" Keith Elder" <柯*** @ removethis.dotnetpimps.net> écritdansle message de

news: eY********************@comcast.com ...


|我不知道如何扩展它,因为GetByName()返回

|列表< ADUser>。尝试这个不行:

|

|列表与LT; DataGridUser> users = DataGridUser.GetByName(" First"," Last");

|

|这会引发一个异常,即GetByName无法将DataGridUser转换为

| ADUser便有。我错过了什么或做错了什么?有人有任何想法吗?


正确。见Jon的回复。但假设您正在创建DataGridUser

实例并将它们添加到List< ADUser>,那么您始终可以将

个别项目投射到DataGridUser。


正如Jon所说,两种类型的通用列表没有关系,它们是兄弟姐妹的b $ b类。您的另一个选择是修改GetByName方法或添加另一个

方法,该方法返回您想要的列表类型。


Joanna
< br $> b $ b -

Joanna Carter [TeamB]

顾问软件工程师


Joanna Carter [TeamB]写道:

" Keith Elder" <柯*** @ removethis.dotnetpimps.net> écritdansle message de
新闻: eY *** *****************@comcast.com ...

|因为GetByName()返回
|,我无法弄清楚如何扩展它列表< ADUser>。尝试这个不起作用:
|
|列表与LT; DataGridUser> users = DataGridUser.GetByName(" First"," Last");
|
|这会引发一个异常,即GetByName无法将DataGridUser转换为
| ADUser便有。我错过了什么或做错了什么?有人有任何想法吗?

正确。见Jon的回复。但假设您正在创建DataGridUser
实例并将它们添加到List< ADUser>,那么您始终可以将
个别项目转换为DataGridUser。

正如Jon所说,两种类型的通用列表没有关系,它们是兄弟级的类。您的另一个选择是修改GetByName方法或添加另一个返回您想要的列表类型的方法。

Joanna



谢谢Joanna,


我认为我的问题在于我真的无法触及这个核心

库ADUser。扩展它是真正修改它的唯一方法。

想想它与System.Data.XXXX没有什么不同。几乎所有的应用程序

都依赖于该对象的工作而很少被修改。


我尝试了向DataGridUser进行转换,但由于方法

GetByName返回List< ADUser>。关注一下你在想什么?b / b

I ran into a unique situation today whereby I have a core library that
uses generics to return users from Active Directory. Example:

List<ADUser> users = ADUser.GetByName("First", "Last");

This works great. However, what I need to do is extend the ADUser
object like so:

class DataGridUser : ADUser
{
// add some new properties
// add some new methods for databinding
}

I can''t figure out how to extend it though since the GetByName() returns
a List<ADUser>. Trying this doesn''t work:

List<DataGridUser> users = DataGridUser.GetByName("First", "Last");

This throws an exception that GetByName cannot convert DataGridUser to
ADUser. What am I missing or doing wrong? Anyone got any ideas?

-Keith

解决方案

Keith Elder wrote:

I ran into a unique situation today whereby I have a core library that
uses generics to return users from Active Directory. Example:

List<ADUser> users = ADUser.GetByName("First", "Last");

This works great. However, what I need to do is extend the ADUser
object like so:

class DataGridUser : ADUser
{
// add some new properties
// add some new methods for databinding
}

I can''t figure out how to extend it though since the GetByName() returns
a List<ADUser>. Trying this doesn''t work:

List<DataGridUser> users = DataGridUser.GetByName("First", "Last");

This throws an exception that GetByName cannot convert DataGridUser to
ADUser. What am I missing or doing wrong? Anyone got any ideas?



Are you sure the compiler doesn''t complain that it can''t convert
List<ADUser> to List<DataGridUser>? The problem is that List<ADUser>
and List<DataGridUser> are entirely different types, even though
DataGridUser derives from ADUser.

There''s a recent thread on this newsgroup about it, called "Casting
generic collections & inheritance". Look on groups.google.com for it if
you don''t have it locally.

If this isn''t what you mean, please provide a short but complete
example which demonstrates your problem. See
http://www.pobox.com/~skeet/csharp/complete.html for more details on
what I mean by that.

Jon


"Keith Elder" <ke***@removethis.dotnetpimps.net> a écrit dans le message de
news: eY********************@comcast.com...

| I can''t figure out how to extend it though since the GetByName() returns
| a List<ADUser>. Trying this doesn''t work:
|
| List<DataGridUser> users = DataGridUser.GetByName("First", "Last");
|
| This throws an exception that GetByName cannot convert DataGridUser to
| ADUser. What am I missing or doing wrong? Anyone got any ideas?

Correct. See Jon''s reply. But assuming that you are creating DataGridUser
instances and adding them to a List<ADUser>, then you can always cast the
individual items to DataGridUser.

As Jon said, the two type of generic list are not related, they are sibling
classes. Your other choice is to modify the GetByName method or add another
method that returns the type of list that you are wanting.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Joanna Carter [TeamB] wrote:

"Keith Elder" <ke***@removethis.dotnetpimps.net> a écrit dans le message de
news: eY********************@comcast.com...

| I can''t figure out how to extend it though since the GetByName() returns
| a List<ADUser>. Trying this doesn''t work:
|
| List<DataGridUser> users = DataGridUser.GetByName("First", "Last");
|
| This throws an exception that GetByName cannot convert DataGridUser to
| ADUser. What am I missing or doing wrong? Anyone got any ideas?

Correct. See Jon''s reply. But assuming that you are creating DataGridUser
instances and adding them to a List<ADUser>, then you can always cast the
individual items to DataGridUser.

As Jon said, the two type of generic list are not related, they are sibling
classes. Your other choice is to modify the GetByName method or add another
method that returns the type of list that you are wanting.

Joanna


Thanks Joanna,

I think my problem lies in the fact that I really cannot touch this core
library ADUser. Extending it is the only way to really modify it.
Think of it no differently than System.Data.XXXX. Pretty much all apps
rely on that object to work and it rarely gets modified.

I did try casting to DataGridUser, however it fails because the method
GetByName returns List<ADUser>. Care to give an example of what you
were thinking?


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

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