实施“是” C ++中的C#,msdn文章 [英] Implementing "Is" C# in C++, msdn article

查看:64
本文介绍了实施“是” C ++中的C#,msdn文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我读过这篇文章
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx 谁首先

我感兴趣很多。

我已经将它翻译为使用泛型而不是模板:


generic< typename T,typename U>

布尔isinst(U u)

{

return safe_cast< T>(u)!= nullptr;

}


但我意识到这不是一个很好的解决方案。例如,

这个例子:


Collections :: ArrayList ^ a = gcnew Collections :: ArrayList();

if (isinst< Collections :: ICollection ^>(a))

Console :: WriteLine(" a is ICollection");


We可以理解safe_cast正在工作,所以函数

返回true。


在阅读本文之前,我曾经用这种方式比较对象: br />

if(a-> GetType()== Collections :: ICollection :: typeid)

Console :: WriteLine(" a是ICollection) ;);


在这个例子中,if语句是false,因为a不是

ICollection。


你能解释一下我错在哪里吗?或者是Msdn文章不完整




提前感谢您的回答,


Nico

解决方案

< ni ************* @ motorola.com>在消息中写道

news:11 ********************* @ i39g2000cwa.googlegro ups.com ...

大家好,

我读过这篇文章
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx 谁首先对我感兴趣。
我已经将它翻译为使用泛型而不是模板:

generic< typename T,typename U>
Boolean isinst(U u)
{
return safe_cast< T>(u)!= nullptr;
}

但我意识到这不是一个很好的解决方案。例如,
这个例子:

Collections :: ArrayList ^ a = gcnew Collections :: ArrayList();
if(isinst< Collections :: ICollection ^>(a ))
Console :: WriteLine(a是ICollection);


这是测试赋值兼容性 - 一个Collections :: ArrayList

实例可以分配给ICollection类型的变量。
我们可以理解safe_cast正在运行,因此函数
返回true。

在阅读本文之前,我曾经用这种方式比较对象:
if(a-> GetType()== Collections :: ICollection :: typeid)
Console :: WriteLine(a是ICollection);

在这个例子中,if语句是假的,因为a不是ICollection。


这是测试类型标识 - Collection :: ArrayList的类型是

,完全是类型Collections :: ArrayList,没有别的。

你能解释一下我错在哪里吗?或者是Msdn文章不完整




你正在测试两种不同的东西,两者在某些情况下都是有效的。

一般来说,赋值兼容性是你感兴趣的。如果你是b $ b检查类型标识,你可能没有正确构造你的代码

,因为类型识别的测试(例如开启类型)通常可以通过虚拟功能更好地处理。


-cd


>你正在测试两种不同的东西,两者在某些情况下都是有效的。

一般来说,赋值兼容性是你感兴趣的。如果你' '
检查类型标识,你可能没有正确构建你的代码,因为类型标识的测试(例如开启类型)通常可以通过虚函数更好地处理。



感谢您的回答,


我已经知道了做完c#,但我认为是关键字与VB.Net中的

相同。


我想在本例中使用它,仅清空文本框控件

在我的表格上:

每个
(控制^ c在此 - >控制中)

if(c-> GetType()= = TextBox :: typeid)

c-> Text ="" ;;

VB中的
,我可能已经使用了Is关键字来了解是否c是Textbox然后

.... ;但也许在C#Is中没有相同的意思。

你觉得我在这里可以避免测试类型标识吗?

你能给我一个例子,测试分配可以

有用吗?通常,我知道我是否可以将某些东西投射到别的东西......

也许在仿制药功能中?


祝你好运,

Nicolas H.


您的代码相当于C#"是是正确的。

ie,

if(a-> GetType()== Collections :: ICollection :: typeid)

等价to:

if(a is Collections :: ICollection)


(关于你的后续帖子,是在C#中是不一样的是在VB中。


MSDN文章,尽管它的标题,并没有解释C ++ / CLI

相当于C# 是。

-

David Anton
www.tangiblesoftwaresolutions.com

即时C#:VB到C#转换器

即时VB:C#到VB转换器

即时C ++:C#到C ++转换器& VB到C ++转换器

即时J#:VB到J#转换器


" ni ************* @ motorola。 COM"写道:

大家好,

我读过这篇文章
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx 谁首先
非常感兴趣。
我把它翻译成使用泛型而不是模板:

generic< typename T,typename U>
Boolean isinst(U u)
{
return safe_cast< T>(u)!= nullptr;
}

但我意识到这不是一个很好的解决方案。例如,
这个例子:

Collections :: ArrayList ^ a = gcnew Collections :: ArrayList();
if(isinst< Collections :: ICollection ^>(a ))
控制台:: WriteLine(a是ICollection);

我们可以理解safe_cast正在工作,因此函数
返回true。 />
在阅读本文之前,我曾经用这种方式比较对象:

if(a-> GetType()== Collections :: ICollection :: typeid)
Console :: WriteLine(a是ICollection);

在这个例子中,if语句是false,因为a不是
ICollection。

你能解释一下我错在哪里吗?或者是Msdn文章不完整


提前感谢您的回答,

Nico



Hi all,

i''ve read this article
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first
interest me much.
I''ve translated it to use generic instead of template :

generic < typename T, typename U >
Boolean isinst(U u)
{
return safe_cast< T >(u) != nullptr;
}

but i realised that this can''t be really a good solution. For example,
this example :

Collections::ArrayList^ a = gcnew Collections::ArrayList();
if ( isinst< Collections::ICollection ^ >(a) )
Console::WriteLine("a is a ICollection ");

We can understand that the safe_cast is working, so the function
returns true.

Before reading this article, I used to compare object with such a way :

if (a->GetType() == Collections::ICollection::typeid)
Console::WriteLine("a is a ICollection ");

In this example, the if statement is false, because a is not a
ICollection.

Can you explain me where i am wrong ? Or is the Msdn article incomplete
?

Thanks in advance for your answers,

Nico

解决方案

<ni*************@motorola.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...

Hi all,

i''ve read this article
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first
interest me much.
I''ve translated it to use generic instead of template :

generic < typename T, typename U >
Boolean isinst(U u)
{
return safe_cast< T >(u) != nullptr;
}

but i realised that this can''t be really a good solution. For example,
this example :

Collections::ArrayList^ a = gcnew Collections::ArrayList();
if ( isinst< Collections::ICollection ^ >(a) )
Console::WriteLine("a is a ICollection ");
This is testing for assignment compatibility - a Collections::ArrayList
instance can be assigned to a variable of type ICollection.

We can understand that the safe_cast is working, so the function
returns true.

Before reading this article, I used to compare object with such a way :

if (a->GetType() == Collections::ICollection::typeid)
Console::WriteLine("a is a ICollection ");

In this example, the if statement is false, because a is not a
ICollection.
This is testing for type identity - the type of a Collection::ArrayList is
exactly the type Collections::ArrayList and nothing else.

Can you explain me where i am wrong ? Or is the Msdn article incomplete
?



You''re testing two different things, both are valid in some context.
Generally, assignment compatibility is what you''re interested in. If you''re
checking for type identity, you''re probably not structuring your code
correctly, as tests for type identity (e.g. switch on type) can usually be
better handled by virtual functions.

-cd


>You''re testing two different things, both are valid in some context.

Generally, assignment compatibility is what you''re interested in. If you''re
checking for type identity, you''re probably not structuring your code
correctly, as tests for type identity (e.g. switch on type) can usually be
better handled by virtual functions.


Thanks for your answer,

I''ve never done c#, but i thought that the "Is" keyword was the same as
in VB.Net.

I thought to use this as in this example, to empty textbox control only
on my form :

for each (Control ^c in this->Controls)
if (c->GetType() == TextBox::typeid)
c->Text = "";

in VB, i probably had used the Is keyword to know if "c Is Textbox Then
...." ; but maybe in C# "Is" hasn''t the same meaning.
Do you think here that I can avoid testing for type identity ?
Could you give me an example where testing for assignement can be
useful ? Usually, i know if I can cast something to something else ...
maybe in generics function ?

Best regards,

Nicolas H.


Your code for the equivalent of the C# "is" is correct.
i.e.,
if (a->GetType() == Collections::ICollection::typeid)
is equivalent to:
if (a is Collections::ICollection)

(regarding your follow-up post, "is" in C# is not the same as "Is" in VB).

The MSDN article, despite it''s title, does not explain the C++/CLI
equivalent to the C# "is".
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"ni*************@motorola.com" wrote:

Hi all,

i''ve read this article
http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first
interest me much.
I''ve translated it to use generic instead of template :

generic < typename T, typename U >
Boolean isinst(U u)
{
return safe_cast< T >(u) != nullptr;
}

but i realised that this can''t be really a good solution. For example,
this example :

Collections::ArrayList^ a = gcnew Collections::ArrayList();
if ( isinst< Collections::ICollection ^ >(a) )
Console::WriteLine("a is a ICollection ");

We can understand that the safe_cast is working, so the function
returns true.

Before reading this article, I used to compare object with such a way :

if (a->GetType() == Collections::ICollection::typeid)
Console::WriteLine("a is a ICollection ");

In this example, the if statement is false, because a is not a
ICollection.

Can you explain me where i am wrong ? Or is the Msdn article incomplete
?

Thanks in advance for your answers,

Nico



这篇关于实施“是” C ++中的C#,msdn文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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