语言增强思路:C#方法实例字段 [英] Language enhancement idea: C# method instance fields

查看:49
本文介绍了语言增强思路:C#方法实例字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时类中的方法需要使用类实例

变量/字段,这些变量/字段不会在方法本身之外使用。

目前这意味着你必须在类中创建一个实例字段,使得从维护站点开始,它与方法断开连接,并且

为其他方法提供了混乱变量的机会当他们

永远不应该。


例如:


公共级MyClass

{


bool dataSent_ = false;


/ *想象其他代码在这里* /


public void SendDataFirstTimeOnly()

{

if(!dataSent_)

{

这样做( );

dataSent_ = true;

}

}


}


注意方法没有与该字段的连接,即使它是唯一应该使用它的方法。
。所以既有维修问题也有b $ b问题,而且这个方法可能会意外地破坏州外的状态。

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

相反,如果我们可以声明这样的类/方法。


public class MyClass

{


/ *想象其他代码在这里* /


public void SendDataFirstTimeOnly()

{

[InstanceField]

bool dataSent_ = false;


if(!dataSent _)

{

做这个();

dataSent_ = true;

}

}


}

这个解决方案的优点是只能通过方法使用的字段


直接连接只能从使用


的方法访问它。这清楚地说明了现场使用情况,并防止在其他地方使用这个值。


我看到的唯一问题是如果你需要克隆对象


声明它可能是一个问题,具体取决于你的对象使用情况。


这种模式的替代方案不是封装的但是更好灵活的是用朋友方法明确地标记变量。

如:


public class MyClass

{


[FriendMethods(SendDataFirstTimeOnly,Clone)]

bool dataSent_ = false;


/ *想象其他代码在这里* /


public void SendDataFirstTimeOnly()

{

if(!dataSent_)

{

做这个();

dataSent_ = true;

}

}


公共对象克隆()

{

MyClass clone = new MyClass()

clone.dataSent_ = dataSent_;

返回n克隆;

}


}

这明确定义/关联哪些方法可以使用

实例字段AND强制执行它,因为如果使用

另一种未列为朋友的方法,则会产生编译错误。

Sometimes a method in a class requires the use of class instance
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such that
from a maintenance stand point it is disconnected from the method, and also
affords the opportunity for other methods to mess with the variable when they
never should.

For example:

public class MyClass
{

bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}

Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.

public class MyClass
{

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;

if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}
The beauty of this solution is the field that is used only by the method

is directly connected to and only accessible from the method that uses

it. This clarifies the field usage clearly and prevents mucking with

this value elsewhere.

The only issues I see with this are if you needed to clone the object

state it could be a problem depending on your objects usage.

An Alternative to this pattern that is not quite as encapsulated but is

more flexible is to explictly tag the variable with friend methods.
Such as:

public class MyClass
{

[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}

}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used by
another method not listed as a friend.

推荐答案

我同意在实例字段上进一步的粒化会很好......在

其实我自己做了..你可以使用XC轻松实现这样的行为#
http://www.resolvecorp.com/ 因为它可以让你实现编译器

扩展名。您还可以执行许多其他有用的操作,例如定义不变量

作为合同的一部分。


干杯,


Greg Young

MVP - C#


" WXS" < WX*@discussions.microsoft.com>在消息中写道

新闻:09 ********************************** @ microsof t.com ...
I agree that further granularization on instance fields would be nice ... in
fact I did it myself .. You can easily implement such behaviors using XC#
http://www.resolvecorp.com/ as it allows you to implement compiler
extensions. You can also do many other useful things like define invariants
as part of your contract.

Cheers,

Greg Young
MVP - C#

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
有时类中的方法需要使用类实例
变量/字段,这些变量/字段不会在方法本身之外使用。
目前这个意味着你必须在类中创建一个实例字段
从维护站点开始它与方法断开连接,并且
还提供了其他方法混乱的机会






公共课MyClass


> bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if(!dataSent_) )
{
这个();
dataSent_ = true;
}
}

}

注意out方法与该字段没有任何关联,即使它是唯一应该使用它的方法。因此,存在维护问题以及从方法中意外破坏外部状态的机会。
----------------- -------------------------------------------------- -
相反如果我们可以宣布这样的类/方法。

公共课MyClass
{

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_ = false;

if(!dataSent _)
{
做这个();
dataSent_ = true;
}
}

}

美女此解决方案是仅由方法使用的字段

直接连接到并且只能从使用

的方法访问。这清楚地阐明了现场使用情况,并防止在其他地方出现这种错误。

我看到的唯一问题是如果你需要克隆对象
<状态可能是一个问题,具体取决于您的对象使用情况。

此模式的替代方案不是封装的,但更灵活的是明确标记带朋友方法的变量。

如:公共类MyClass


[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if( !dataSent_)
{
执行此操作();
dataSent_ = true;
}
}
公共对象Clone()
{
MyClass clone = new MyClass()
clone.dataSent_ = dataSent_;
返回克隆;
}

}

这明确定义/关联哪些方法可以使用
stance字段AND强制执行它,因为如果使用

另一种未列为朋友的方法,则会产生编译错误。
Sometimes a method in a class requires the use of class instance
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such
that
from a maintenance stand point it is disconnected from the method, and
also
affords the opportunity for other methods to mess with the variable when
they
never should.

For example:

public class MyClass
{

bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}

Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.

public class MyClass
{

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;

if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}
The beauty of this solution is the field that is used only by the method

is directly connected to and only accessible from the method that uses

it. This clarifies the field usage clearly and prevents mucking with

this value elsewhere.

The only issues I see with this are if you needed to clone the object

state it could be a problem depending on your objects usage.

An Alternative to this pattern that is not quite as encapsulated but is

more flexible is to explictly tag the variable with friend methods.
Such as:

public class MyClass
{

[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}

}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used
by
another method not listed as a friend.



我也称之为[RestrictToMethod(方法)],因为它比朋友更有意义。这有着截然不同的意义。


干杯,


Greg Young

MVP - C#

" WXS" < WX*@discussions.microsoft.com>在消息中写道

新闻:09 ********************************** @ microsof t.com ...
Also I might call it a [RestrictToMethod(method)] as it makes a bit more
sense than "Friend" which has a much different meaning.

Cheers,

Greg Young
MVP - C#
"WXS" <WX*@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
有时类中的方法需要使用类实例
变量/字段,这些变量/字段不会在方法本身之外使用。
目前这个意味着你必须在类中创建一个实例字段
从维护站点开始它与方法断开连接,并且
还提供了其他方法混乱的机会






公共课MyClass


> bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if(!dataSent_) )
{
这个();
dataSent_ = true;
}
}

}

注意out方法与该字段没有任何关联,即使它是唯一应该使用它的方法。因此,存在维护问题以及从方法中意外破坏外部状态的机会。
----------------- -------------------------------------------------- -
相反如果我们可以宣布这样的类/方法。

公共课MyClass
{

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_ = false;

if(!dataSent _)
{
做这个();
dataSent_ = true;
}
}

}

美女此解决方案是仅由方法使用的字段

直接连接到并且只能从使用

的方法访问。这清楚地阐明了现场使用情况,并防止在其他地方出现这种错误。

我看到的唯一问题是如果你需要克隆对象
<状态可能是一个问题,具体取决于您的对象使用情况。

此模式的替代方案不是封装的,但更灵活的是明确标记带朋友方法的变量。

如:公共类MyClass


[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if( !dataSent_)
{
执行此操作();
dataSent_ = true;
}
}
公共对象Clone()
{
MyClass clone = new MyClass()
clone.dataSent_ = dataSent_;
返回克隆;
}

}

这明确定义/关联哪些方法可以使用
stance字段AND强制执行它,因为如果使用

另一种未列为朋友的方法,则会产生编译错误。
Sometimes a method in a class requires the use of class instance
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such
that
from a maintenance stand point it is disconnected from the method, and
also
affords the opportunity for other methods to mess with the variable when
they
never should.

For example:

public class MyClass
{

bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}

Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.

public class MyClass
{

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;

if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}
The beauty of this solution is the field that is used only by the method

is directly connected to and only accessible from the method that uses

it. This clarifies the field usage clearly and prevents mucking with

this value elsewhere.

The only issues I see with this are if you needed to clone the object

state it could be a problem depending on your objects usage.

An Alternative to this pattern that is not quite as encapsulated but is

more flexible is to explictly tag the variable with friend methods.
Such as:

public class MyClass
{

[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}

}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used
by
another method not listed as a friend.



我认为有时候这会有所帮助。


你应该去产品反馈中心并提出建议,

然后发布链接这里的建议让人们可以投票:

http://lab.msdn.microsoft.com/productfeedback/


实际上并不难,只会更多

" compiler"魔法。 VB使用static关键字执行此操作,其中编译器

修改变量的名称,然后将其用作类中的字段。


此外,它必须是一个应用于变量

声明的关键字,而不是属性,因为属性在

方法中无效。你需要这样的东西:


public void DoSomething()

{

//表明这应该是静态的方法。

static int myVar = 0;

}


这也可能表明这是在课程设置时设置的

创建,而不是第一次使用。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" WXS" < WX*@discussions.microsoft.com>在消息中写道

新闻:09 ********************************** @ microsof t.com ...
I think that there are times that this would be helpful.

You should go to the Product Feedback Center and put in the suggestion,
then post the link to the suggestion here so that people can vote on it:

http://lab.msdn.microsoft.com/productfeedback/

It wouldn''t be that hard to do really, it would just be some more
"compiler" magic. VB does this with the static keyword, where the compiler
mangles the name of the variable and then uses it as a field in the class.

Also, it would have to be a keyword that is applied to the variable
declaration, not an attribute, since attributes are not valid inside
methods. You would need something like:

public void DoSomething()
{
// Indicate that this should be static inside the method.
static int myVar = 0;
}

This would also probably indicate that this is set when the class is
created, not on its first use.

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

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
有时类中的方法需要使用类实例
变量/字段,这些变量/字段不会在方法本身之外使用。
目前这个意味着你必须在类中创建一个实例字段
从维护站点开始它与方法断开连接,并且
还提供了其他方法混乱的机会






公共课MyClass


> bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if(!dataSent_) )
{
这个();
dataSent_ = true;
}
}

}

注意out方法与该字段没有任何关联,即使它是唯一应该使用它的方法。因此,存在维护问题以及从方法中意外破坏外部状态的机会。
----------------- -------------------------------------------------- -
相反如果我们可以宣布这样的类/方法。

公共课MyClass
{

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_ = false;

if(!dataSent _)
{
做这个();
dataSent_ = true;
}
}

}

美女此解决方案是仅由方法使用的字段

直接连接到并且只能从使用

的方法访问。这清楚地阐明了现场使用情况,并防止在其他地方出现这种错误。

我看到的唯一问题是如果你需要克隆对象
<状态可能是一个问题,具体取决于您的对象使用情况。

此模式的替代方案不是封装的,但更灵活的是明确标记带朋友方法的变量。

如:公共类MyClass


[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_ = false;

/ *想象其他代码在这里* /

public void SendDataFirstTimeOnly()
{
if( !dataSent_)
{
执行此操作();
dataSent_ = true;
}
}
公共对象Clone()
{
MyClass clone = new MyClass()
clone.dataSent_ = dataSent_;
返回克隆;
}

}

这明确定义/关联哪些方法可以使用
stance字段AND强制执行它,因为如果使用

另一种未列为朋友的方法,则会产生编译错误。
Sometimes a method in a class requires the use of class instance
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such
that
from a maintenance stand point it is disconnected from the method, and
also
affords the opportunity for other methods to mess with the variable when
they
never should.

For example:

public class MyClass
{

bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}

Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.

public class MyClass
{

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;

if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

}
The beauty of this solution is the field that is used only by the method

is directly connected to and only accessible from the method that uses

it. This clarifies the field usage clearly and prevents mucking with

this value elsewhere.

The only issues I see with this are if you needed to clone the object

state it could be a problem depending on your objects usage.

An Alternative to this pattern that is not quite as encapsulated but is

more flexible is to explictly tag the variable with friend methods.
Such as:

public class MyClass
{

[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;

/* IMAGINE TONS OF OTHER CODE HERE */

public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}

public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}

}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used
by
another method not listed as a friend.



这篇关于语言增强思路:C#方法实例字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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