从基类转换[很长] [英] converting from base class [quite long]

查看:54
本文介绍了从基类转换[很长]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好C#gurus :)


我有转换问题。


我有一堂课我不能改变 - 这是一个DataColumn类;


我有一个包装类。


public MyDataColumn:DataColumn

{

public bool MyFlag;

}


我想做这样的事情:


DataTable表;

foreach(table.Columns中的MyDataColumn列){

//做某事;

}


---


之前我有以下构造:


public MyDataColumn {

private DataColumn wrappedColumn;

private MyDataColumn(DataColumn column)

{this.wrappedColumn = column; } $ / $
公共静态隐式MyDataColumn(DataColumn列)

{返回新的MyDataColumn(列); }

}


这样我就可以做到以下几点:


DataTable表;

foreach(table.Columns中的DataColumn列){

MyDataColumn wrapped = column; //无需明确转换

//做点什么

}

----

但是我想直接继承会更优雅(如第一个例子中的
)。问题很明显,代码编译没有

问题(有定义的转换器,从基础到继承类时,你定义类是
。)但是它不会执行也很明显如何可以一个基础

构造函数构建一个子类对象。


解决方案是使用转换器,但这里的编译器是prostest,

如果存在一个转换器就无法编译转换器,如果这是显式或隐式转换器,则不会产生任何问题,根据标准

(在网上找到的地方),一个人无法定义从基础转换

类。


有谁知道如何解决它?如果问题无法解决,为什么要编译?
编译?

谢谢,

CUIN Kaczy

Hi C# gurus :)

I have a conversion problem.

I have a class that I can''t change - it''s a DataColumn class;

I have a wrapper class around it.

public MyDataColumn : DataColumn
{
public bool MyFlag;
}

I''d like to do something like this:

DataTable table;
foreach (MyDataColumn column in table.Columns) {
// do something;
}

---

previously I had following construct:

public MyDataColumn {
private DataColumn wrappedColumn;
private MyDataColumn(DataColumn column)
{ this.wrappedColumn = column; }
public static implicit MyDataColumn(DataColumn column)
{ return new MyDataColumn(column); }
}

this way I was able to do the following:

DataTable table;
foreach (DataColumn column in table.Columns) {
MyDataColumn wrapped = column; // no explicit conversion required
// do something
}
----
however I thought that it would be more elegant to inherit directly (as in
the first example). The problem is obvious, the code compile without
problems (there are predefined converters from base to inheriting class when
you define class.) However it will not execute also obvious how can a base
constructor build a child class object.

The solution would be to use the converters, but here the compiler prostest,
that it can''t compile a converter when there is one present it doesn''t
matter if this is explicit or implicit converter, according to standard
(found somewhere on the web), one cannot define a conversion from a base
class.

Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?
thanks,
CUIN Kaczy

推荐答案

Andrzej Kaczmarczyk写道:


< snip>
Andrzej Kaczmarczyk wrote:

<snip>
解决方案是使用转换器,但是这里的编译器有利于它,根据标准,如果存在一个转换器它就不能编译转换器,根据标准
(在网上的某个地方找到了,人们无法定义基础类的转换。

有谁知道如何解决它?如果问题无法解决,为什么要编译?
The solution would be to use the converters, but here the compiler prostest,
that it can''t compile a converter when there is one present it doesn''t
matter if this is explicit or implicit converter, according to standard
(found somewhere on the web), one cannot define a conversion from a base
class.

Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?




它正在编译,因为它完全可能(就编译器而言)

知道)table.Columns中的每个元素都是MyDataColumn。只是

发生在这里不是真的。


您需要使用以前的解决方案之一。


Jon



It is compiling because it''s perfectly possible (as far as the compiler
knows) for every element in table.Columns to be a MyDataColumn. It just
happens that it''s not true here.

You need to use one of your previous solutions.

Jon


或者他可以测试每一栏:


DataTable表;

DataColumn列;

MyDataColumn myColumn;


for(int i = 0; i< table.Columns.Count; i ++)

{

myColumn = table.Columns [i] as MyDataColumn;

if(myColumn!= null)

{

//做东西

}

}


HTH

Ollie Riches

" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...
or he could test each column:

DataTable table;
DataColumn column;
MyDataColumn myColumn;

for(int i = 0; i < table.Columns.Count; i++)
{
myColumn = table.Columns[i] as MyDataColumn;
if(myColumn != null)
{
// do stuff
}
}

HTH

Ollie Riches
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Andrzej Kaczmarczyk写道:

< snip>
Andrzej Kaczmarczyk wrote:

<snip>
解决方法是使用转换器,但这里编译器
prostest,
根据标准
(在网络上的某个地方找到),当有一个存在时它不能编译转换器,如果这是显式或隐式转换器则不重要,无法定义基础类的转换。

有谁知道如何解决它?如果问题无法解决,为什么要进行编译?
The solution would be to use the converters, but here the compiler
prostest,
that it can''t compile a converter when there is one present it doesn''t
matter if this is explicit or implicit converter, according to standard
(found somewhere on the web), one cannot define a conversion from a base
class.

Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?



它正在编译,因为它完全可能(就编译器所知) table.Columns中的元素是MyDataColumn。只是
发生在这里不是真的。

你需要使用以前的一个解决方案。

Jon



It is compiling because it''s perfectly possible (as far as the compiler
knows) for every element in table.Columns to be a MyDataColumn. It just
happens that it''s not true here.

You need to use one of your previous solutions.

Jon


Andrzej,


见内联:
Andrzej,

See inline:
我有一个包装类。<公共MyDataColumn:DataColumn
{
public bool MyFlag;
}


这不是包装类。您已经扩展了DataColumn类。如果

你把它包装起来,你会在

MyDataColumn类中有一个DataColumn实例。

我想做类似这样的事情:

DataTable表;
foreach(table.Columns中的MyDataColumn列){
//做某事;
}


执行此操作的唯一方法是从头开始构建表。你需要通过调用
$的重载来将你的类型实例(MyDataColumn)添加到列

集合(通过Columns属性公开)中。 b $ b带有DataColumn实例的Add方法。然后你应该能够

来正常填写这个DataTable。


之后,当访问列时,你必须在

为了使用你的类型的特定属性。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

---

之前我有以下构造:

公共MyDataColumn {
私有DataColumn wrappedColumn;
private MyDataColumn(DataColumn column)
{this.wrappedColumn = column;公共静态隐式MyDataColumn(DataColumn列)
{return new MyDataColumn(column);这样我就可以做到以下几点:

DataTable表;
foreach(table.Columns中的DataColumn列){
MyDataColumn wrapped = column; //不需要明确的转换
//做点什么
}

----
然而我认为直接继承会更优雅(如
第一个例子)。问题很明显,代码编译时没有问题(当你定义类时,有预定义的转换器从基类到继承类。)但是它不会执行也很明显怎么可以基础构造函数构建一个子类对象。

解决方案是使用转换器,但这里编译器
prostest,当有一个转换器时它不能编译转换器标准(在网上某处找到),无法定义从基类转换。

谢谢,
CUIN Kaczy
I have a wrapper class around it.

public MyDataColumn : DataColumn
{
public bool MyFlag;
}
This isn''t a wrapper class. You have extended the DataColumn class. If
you wrapped it, you would have an instance of the DataColumn inside of the
MyDataColumn class.

I''d like to do something like this:

DataTable table;
foreach (MyDataColumn column in table.Columns) {
// do something;
}
The only way to do this is to construct the table from scratch. You
will have to add the instances of your type (MyDataColumn) to the columns
collection (exposed through the Columns property) by calling the overload of
the Add method which takes a DataColumn instance. You should then be able
to fill this DataTable normally.

Later, when accessing the columns, you will have to cast to your type in
order to use your type''s specific properties.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

---

previously I had following construct:

public MyDataColumn {
private DataColumn wrappedColumn;
private MyDataColumn(DataColumn column)
{ this.wrappedColumn = column; }
public static implicit MyDataColumn(DataColumn column)
{ return new MyDataColumn(column); }
}

this way I was able to do the following:

DataTable table;
foreach (DataColumn column in table.Columns) {
MyDataColumn wrapped = column; // no explicit conversion required
// do something
}
----
however I thought that it would be more elegant to inherit directly (as in
the first example). The problem is obvious, the code compile without
problems (there are predefined converters from base to inheriting class
when you define class.) However it will not execute also obvious how can a
base constructor build a child class object.

The solution would be to use the converters, but here the compiler
prostest, that it can''t compile a converter when there is one present it
doesn''t matter if this is explicit or implicit converter, according to
standard (found somewhere on the web), one cannot define a conversion from
a base class.

Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?
thanks,
CUIN Kaczy



这篇关于从基类转换[很长]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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