隐式转换设计问题 [英] Implicit conversion design issues

查看:59
本文介绍了隐式转换设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解来自对象的隐式类型转换 - >字符串和

反之亦然。


我有两个类,一个是Driver,另一个是StringWrapper。这些只是

测试类,试图模仿我试图在

现有项目中遵循的模式。


这些是我的步骤:

1)我有一个方法printMe存在于应用程序中的原本

用于接收字符串。这个方法是静态的,位于Driver

类中。我无法删除此方法,因为它是公开的,并且在整个

应用程序中使用。

2)现在,我意识到我需要做一些解析和清理

字符串在方法之前可以继续执行其逻辑(printMe)。

3)我的解决方案是更改方法sig以接受StringWrapper

类,由我创建,它将接受一个隐式字符串并返回一个带有清理过的字符串的

StringWrapper对象。

4)现在,当StringWapper对象由方法中的代码使用

printMe,我想将其视为String对象而不是StringWrapper

对象。所以我所做的是创建另一个从
StringWrapper到String的隐式转换。


我希望这在这一点上有意义。本质上是为了最小化代码

调用我的printMe方法的代码中的更改,以便它只知道

String对象。并且printMe函数中的提醒逻辑也应该只知道String对象的
。所以StringWrapper类基本上将
转换为字符串到它的类型的对象,然后清理字符串和

然后,无论在printMe方法中使用StringWrapper实例,它将

应该将自己暴露为String。


所以我的问题是

1)在我的printMe方法中,我有一个名为

Console.Write(val.ToLower())的声明。这让我有一个例外,因为它说

StringWrapper不支持ToLower方法。为什么即使我有
有隐式转换?

2)如果我做Console.Write(((string)val).ToLower());有用!!即使

这是一个明确的转换,我还没有定义任何明确的

转换....


我有点丢失在这里....任何人都可以帮我解决这个问题吗?


谢谢,

Girish


驱动程序类

-------------

使用系统;

命名空间test1 {

类驱动程序{

//旧函数sig是; static public void printMe(String val)

static public void printMe(StringWrapper val){

//Console.Write(val.ToLower()); // old

Console.Write(((string)val).ToLower());

}


[STAThread ]

static void Main(string [] args){

String b =" Testing 123"

Driver.printMe(b) ;


//退出前等待输入

Console.Read();

}

}

}

StringWrapper类

---------------------

使用System;

命名空间test1 {

公共类StringWrapper {

private String _val;


public String val {

get {return _val; } b / b $

public StringWrapper(String val){

this._val = val;

}


静态公共隐式运算符StringWrapper(String val){

val + =" (我改变了)" ;;

返回新的StringWrapper(val);

}


静态公共隐式运算符字符串(StringWrapper) val){

返回val.val;

}

}

}

Im trying to understand implicit type conversions from object -> string and
vice versa.

I have two classes, one Driver and one called StringWrapper. These are just
test classes that try and emulate the pattern im trying to follow in an
existing project.

These are my steps:
1) I have a method "printMe" existing in the application which originally
used to take in a string. This method is static and sits in the Driver
class. I cannot remove this method since it public and is used all over the
application.
2) Now, Ive realised that I need to do some parsing and cleaning up of the
string before the method can continue to execute its logic (printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.

I hope this makes sense at this point. Essentially Im tring to minimize code
changes in the code that calls my printMe method so that it knows of only
String objects. AND the reminaing logic in printMe function should only know
of String objects as well. So the StringWrapper class will essentially
convert string to an object of its type, do the cleaning of the string and
then, whereever the StringWrapper instance is used in printMe method, it
should expose itself as a String instead.

So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said the
StringWrapper does not support the ToLower method. How come even though I
have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even though
this is an explicit conversion and I havent defined any explicit
conversions....

Im kinda lost here.... can anyone help me with a solution to this problem?

Thanks,
Girish

Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);

//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }
}

public StringWrapper(String val) {
this._val = val;
}

static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}

static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}

推荐答案

对于那些认为我没有附加任何代码的人,因为我写了一封很长的

电子邮件,你可以找到底部的代码我之前的帖子。我知道一些

人们一定要去...好吧我正在阅读所有这些东西..所以这个代码现在是什么?b $ b现在???


:-) :-) :-) :-)

" Girish" < GB **** @ tietronixinc.com>在消息中写道

news:e%**************** @ TK2MSFTNGP10.phx.gbl ...
For those of you who thought I didnt attach any code, since ive wrote a long
email, you can find the code at the bottom of my earlier post. I know some
people must be going... OK im reading all this stuff.. so wheres the code
now???

:-):-):-):-)
"Girish" <gb****@tietronixinc.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
我正在尝试理解来自对象的隐式类型转换 - >字符串
反之亦然。

我有两个类,一个是Driver,另一个是StringWrapper。这些只是测试类,试图模仿我试图在现有项目中遵循的模式。

这些是我的步骤:
1)我有一个方法printMe存在于最初用于接收字符串的应用程序中。此方法是静态的,位于Driver
类中。我不能删除这个方法,因为它是公开的并且在应用程序中使用。
2)现在,我已经意识到我需要在方法之前做一些解析和清理
字符串可以继续执行它的逻辑(printMe)。
3)我的解决方案是改变方法sig以接受我创建的StringWrapper类,它将接受一个隐式字符串并返回一个
带有清理过的字符串的StringWrapper对象。
4)现在,当方法
printMe中的代码使用StringWapper对象时,我想把它当作String对象而不是StringWrapper对象。所以我所做的就是创建另一个从StringWrapper到String的隐式转换。

我希望这在这一点上有意义。本质上是为了尽量减少调用我的printMe方法的代码中的代码更改,以便它只知道
String对象。并且printMe函数中的提醒逻辑也应该只知道String对象。所以StringWrapper类将
实质上将字符串转换为其类型的对象,清除字符串然后,无论在printMe方法中使用StringWrapper实例,它都应该暴露自己相反,我的问题是

1)在我的printMe方法中,我有一个名为
Console.Write(val.ToLower())的声明。这让我有一个例外,因为它说StringWrapper不支持ToLower方法。怎么来,虽然我有隐式转换?
2)如果我做Console.Write(((string)val).ToLower());有用!!即使这是一个明确的转换,我还没有定义任何明确的转换....

我有点迷失在这里......任何人都可以帮我解决问题这个问题?

谢谢,
Girish

驱动程序类
-------------
使用系统;
命名空间test1 {
类驱动程序{
//旧功能sig是; static public void printMe(String val)
static public void printMe(StringWrapper val){
//Console.Write(val.ToLower()); // old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string [] args){
字符串b ="测试123"
Driver.printMe(b);

//退出前等待输入
Console.Read( );
}
}
}
StringWrapper类
------------------- -
使用System;
命名空间test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }

公共StringWrapper(String val){
this._val = val;
}
静态公共隐式运算符StringWrapper(字符串val){
val + =" (我改变了)" ;;
返回新的StringWrapper(val);

静态公共隐式运算符字符串(StringWrapper val){
return val.val;
}
}
}
Im trying to understand implicit type conversions from object -> string
and vice versa.

I have two classes, one Driver and one called StringWrapper. These are
just test classes that try and emulate the pattern im trying to follow in
an existing project.

These are my steps:
1) I have a method "printMe" existing in the application which originally
used to take in a string. This method is static and sits in the Driver
class. I cannot remove this method since it public and is used all over
the application.
2) Now, Ive realised that I need to do some parsing and cleaning up of the
string before the method can continue to execute its logic (printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.

I hope this makes sense at this point. Essentially Im tring to minimize
code changes in the code that calls my printMe method so that it knows of
only String objects. AND the reminaing logic in printMe function should
only know of String objects as well. So the StringWrapper class will
essentially convert string to an object of its type, do the cleaning of
the string and then, whereever the StringWrapper instance is used in
printMe method, it should expose itself as a String instead.

So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said
the StringWrapper does not support the ToLower method. How come even
though I have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even though
this is an explicit conversion and I havent defined any explicit
conversions....

Im kinda lost here.... can anyone help me with a solution to this problem?

Thanks,
Girish

Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);

//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }
}

public StringWrapper(String val) {
this._val = val;
}

static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}

static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}



哦......好吧,这对我来说似乎很简单。但我可能会错过一些

应用程序specfic ..如果你能够更改签名..我假设你

有源代码..为什么不能你把清理代码写成了printMe的前几行

行并简化了吗?


VJ


Girish < GB **** @ tietronixinc.com>在消息中写道

news:%2 ****************** @ TK2MSFTNGP10.phx.gbl ...
Oh ... Ok maybe this seems simple to me.. but I might miss something
application specfic.. if you are able to change the signature.. I assume you
have the source code.. why can''t you write the cleanup code as the first few
lines of the printMe and make it simple?

VJ

"Girish" <gb****@tietronixinc.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
对于那些认为我没有附加任何代码的人,因为我写了一封很长的电子邮件,你可以在我之前的帖子的底部找到代码。我知道
有些人一定要去...好吧我正在阅读所有这些东西..所以现在是
代码???

: - ): - ): - ):-)

" Girish" < GB **** @ tietronixinc.com>在消息中写道
新闻:e%**************** @ TK2MSFTNGP10.phx.gbl ...
For those of you who thought I didnt attach any code, since ive wrote a
long email, you can find the code at the bottom of my earlier post. I know
some people must be going... OK im reading all this stuff.. so wheres the
code now???

:-):-):-):-)
"Girish" <gb****@tietronixinc.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
我试图理解隐式类型来自对象的转换 - >字符串
反之亦然。

我有两个类,一个是Driver,另一个是StringWrapper。这些只是测试类,试图模仿我试图在现有项目中遵循的模式。

这些是我的步骤:
1)我有一个方法printMe存在于最初用于接收字符串的应用程序中。此方法是静态的,位于Driver
类中。我不能删除这个方法,因为它是公开的并且在应用程序中使用。
2)现在,我已经意识到我需要在方法之前做一些解析和清理字符串可以继续执行它的逻辑(printMe)。
3)我的解决方案是改变方法sig以接受我创建的StringWrapper类,它将接受一个隐式字符串并返回一个
带有清理过的字符串的StringWrapper对象。
4)现在,当方法
printMe中的代码使用StringWapper对象时,我想把它当作String对象而不是StringWrapper对象。所以我所做的就是创建另一个从StringWrapper到String的隐式转换。

我希望这在这一点上有意义。本质上是为了尽量减少调用我的printMe方法的代码中的代码更改,以便它只知道
String对象。并且printMe函数中的提醒逻辑也应该只知道String对象。所以StringWrapper类将
实质上将字符串转换为其类型的对象,清除字符串然后,无论在printMe方法中使用StringWrapper实例,它都应该暴露自己相反,我的问题是

1)在我的printMe方法中,我有一个名为
Console.Write(val.ToLower())的声明。这让我有一个例外,因为它说StringWrapper不支持ToLower方法。怎么来,虽然我有隐式转换?
2)如果我做Console.Write(((string)val).ToLower());有用!!即使这是一个明确的转换,我还没有定义任何明确的转换....

我有点迷失在这里......任何人都可以帮我解决问题这个
问题?

谢谢,
Girish

驱动程序类
------------- <使用System;
命名空间test1 {
类驱动程序{
//旧功能sig是; static public void printMe(String val)
static public void printMe(StringWrapper val){
//Console.Write(val.ToLower()); // old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string [] args){
字符串b ="测试123"
Driver.printMe(b);

//退出前等待输入
Console.Read( );
}
}
}
StringWrapper类
------------------- -
使用System;
命名空间test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }

公共StringWrapper(String val){
this._val = val;
}
静态公共隐式运算符StringWrapper(字符串val){
val + =" (我改变了)" ;;
返回新的StringWrapper(val);

静态公共隐式运算符字符串(StringWrapper val){
return val.val;
}
}

Im trying to understand implicit type conversions from object -> string
and vice versa.

I have two classes, one Driver and one called StringWrapper. These are
just test classes that try and emulate the pattern im trying to follow in
an existing project.

These are my steps:
1) I have a method "printMe" existing in the application which originally
used to take in a string. This method is static and sits in the Driver
class. I cannot remove this method since it public and is used all over
the application.
2) Now, Ive realised that I need to do some parsing and cleaning up of
the string before the method can continue to execute its logic (printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.

I hope this makes sense at this point. Essentially Im tring to minimize
code changes in the code that calls my printMe method so that it knows of
only String objects. AND the reminaing logic in printMe function should
only know of String objects as well. So the StringWrapper class will
essentially convert string to an object of its type, do the cleaning of
the string and then, whereever the StringWrapper instance is used in
printMe method, it should expose itself as a String instead.

So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said
the StringWrapper does not support the ToLower method. How come even
though I have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even though
this is an explicit conversion and I havent defined any explicit
conversions....

Im kinda lost here.... can anyone help me with a solution to this
problem?

Thanks,
Girish

Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);

//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }
}

public StringWrapper(String val) {
this._val = val;
}

static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}

static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}




是的,我可以有。但是对于未来,我可以让每个人都知道,从现在开始使用

StringWrapper类进行字符串复制。

这比告诉大家更容易,嘿什么时候你在

的未来写一个方法,记得在任何其他代码之前调用这个其他帮助函数并确保它被称为




我不知道,但我通常喜欢有不止一种做事方式

并选择适当的......如果隐含的演员方法不是这样的话

去...如果有人能帮我理解原因,我真的很感激。 :)


谢谢!

Girish


" VJ" < 6 ******** @ yahoo.com>在消息中写道

新闻:uB ************** @ TK2MSFTNGP15.phx.gbl ...
True, I could have. But for the future, I can just let everyone know, use
the StringWrapper class from now on people for your string manuplilation.
This is easier than telling everyone, hey when you write a method in the
future, remember to call this other helper function and make sure its called
before any other code.

I dont know, but i usually like to have more than one way of doing things
and pick whats appropriate.... and if the implicit cast mehod is not the way
to go... id really appreciate it if someone can help me understand why. :)

thanks!
Girish

"VJ" <vi********@yahoo.com> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...
哦......好的也许这对我来说似乎很简单..但我可能会错过一些应用程序特定的东西......如果你能够改变签名......我假设你有源代码..为什么你不能将清理代码写为printMe的前几行,并使其简单化?

VJ

Girish < GB **** @ tietronixinc.com>在消息中写道
新闻:%2 ****************** @ TK2MSFTNGP10.phx.gbl ...
Oh ... Ok maybe this seems simple to me.. but I might miss something
application specfic.. if you are able to change the signature.. I assume
you have the source code.. why can''t you write the cleanup code as the
first few lines of the printMe and make it simple?

VJ

"Girish" <gb****@tietronixinc.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
对于你们这些人谁以为我没有附上任何代码,因为我写了一封很长的电子邮件,你可以在我早期帖子的底部找到代码。我知道有些人一定要去...好吧我正在阅读所有这些东西..所以现在代码是什么?

: - ): - ): - ):-)

" Girish" < GB **** @ tietronixinc.com>在消息中写道
新闻:e%**************** @ TK2MSFTNGP10.phx.gbl ...
For those of you who thought I didnt attach any code, since ive wrote a
long email, you can find the code at the bottom of my earlier post. I
know some people must be going... OK im reading all this stuff.. so
wheres the code now???

:-):-):-):-)
"Girish" <gb****@tietronixinc.com> wrote in message
news:e%****************@TK2MSFTNGP10.phx.gbl...
我试图理解隐式类型来自对象的转换 - >字符串
反之亦然。

我有两个类,一个是Driver,另一个是StringWrapper。这些只是测试类,试图模拟我试图在现有项目中关注
的模式。

这些是我的步骤:
1)我有一个方法printMe存在于最初用于接收字符串的应用程序中。这个方法是静态的,位于Driver类中。我不能删除这个方法,因为它公开并且在整个应用程序中都被使用。
2)现在,我已经意识到我需要在方法之前做一些解析和清理字符串可以继续执行其逻辑
(printMe)。
3)我的解决方案是改变方法sig以接受由我创建的StringWrapper类,它将采用隐式字符串并返回一个带有清理过的字符串的StringWrapper对象。
4)现在,当方法
printMe中的代码使用StringWapper对象时,我想将其视为String对象而不是StringWrapper
对象。所以我所做的就是创建另一个从StringWrapper到String的隐式转换。

我希望这在这一点上有意义。本质上是为了尽量减少调用我的printMe方法的代码中的代码更改,以便它只知道String对象。并且printMe函数中的提醒逻辑
也应该只知道String对象。所以StringWrapper类将基本上将字符串转换为其类型的对象,对字符串执行
清理,然后,无论在printMe方法中使用StringWrapper实例,它都应该暴露自己相反,我的问题是

1)在我的printMe方法中,我有一个名为
Console.Write(val.ToLower())的声明。这让我有一个例外,因为它说StringWrapper不支持ToLower方法。怎么来,虽然我有隐式转换?
2)如果我做Console.Write(((string)val).ToLower());有用!!即使
虽然这是一个明确的转换,我还没有定义任何明确的转换....

我有点迷失在这里......任何人都可以帮我解决问题这个
问题?

谢谢,
Girish

驱动程序类
------------- <使用System;
命名空间test1 {
类驱动程序{
//旧功能sig是; static public void printMe(String val)
static public void printMe(StringWrapper val){
//Console.Write(val.ToLower()); // old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string [] args){
字符串b ="测试123"
Driver.printMe(b);

//退出前等待输入
Console.Read( );
}
}
}
StringWrapper类
------------------- -
使用System;
命名空间test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }

公共StringWrapper(String val){
this._val = val;
}
静态公共隐式运算符StringWrapper(字符串val){
val + =" (我改变了)" ;;
返回新的StringWrapper(val);

静态公共隐式运算符字符串(StringWrapper val){
return val.val;
}
}
Im trying to understand implicit type conversions from object -> string
and vice versa.

I have two classes, one Driver and one called StringWrapper. These are
just test classes that try and emulate the pattern im trying to follow
in an existing project.

These are my steps:
1) I have a method "printMe" existing in the application which
originally used to take in a string. This method is static and sits in
the Driver class. I cannot remove this method since it public and is
used all over the application.
2) Now, Ive realised that I need to do some parsing and cleaning up of
the string before the method can continue to execute its logic
(printMe).
3) My solution was to change the method sig to take in a StringWrapper
class, created by me, that will take in an implicit string and return a
StringWrapper object with the cleaned up string.
4) Now, when the StringWapper object is used by the code in the method
printMe, I want to treat it as a String object and not a StringWrapper
object. So what Ive done is create another implicit conversion from
StringWrapper to String.

I hope this makes sense at this point. Essentially Im tring to minimize
code changes in the code that calls my printMe method so that it knows
of only String objects. AND the reminaing logic in printMe function
should only know of String objects as well. So the StringWrapper class
will essentially convert string to an object of its type, do the
cleaning of the string and then, whereever the StringWrapper instance is
used in printMe method, it should expose itself as a String instead.

So my question is
1) In my printMe method, I had a statement called
Console.Write(val.ToLower()). This threw me an exception because it said
the StringWrapper does not support the ToLower method. How come even
though I have an implicit conversion?
2) If I do Console.Write(((string)val).ToLower()); it works!! Even
though this is an explicit conversion and I havent defined any explicit
conversions....

Im kinda lost here.... can anyone help me with a solution to this
problem?

Thanks,
Girish

Driver Class
-------------
using System;
namespace test1 {
class Driver {
//old func sig was; static public void printMe(String val)
static public void printMe(StringWrapper val) {
//Console.Write(val.ToLower()); //old
Console.Write(((string)val).ToLower());
}

[STAThread]
static void Main(string[] args) {
String b = "Testing 123";
Driver.printMe(b);

//wait for input before exiting
Console.Read();
}
}
}
StringWrapper Class
---------------------
using System;
namespace test1 {
public class StringWrapper {
private String _val;

public String val {
get { return _val; }
}

public StringWrapper(String val) {
this._val = val;
}

static public implicit operator StringWrapper(String val) {
val += " (im changed)";
return new StringWrapper(val);
}

static public implicit operator string(StringWrapper val) {
return val.val;
}
}
}




这篇关于隐式转换设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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