寻找其他C#程序员关于部分方法的想法 [英] Looking for other C# programmers thoughts on partial methods

查看:52
本文介绍了寻找其他C#程序员关于部分方法的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个跨越代码中两个部分类的类,这里是'

一个例子(不要读太多,代码是不实用的

使用,这只是我混淆发生的一个简单例子。


//在SharedClassExample1.cs内部

公共部分类SharedClassExample

{


public List< stringBooksOnShelf {get;组; }

public List< stringBooksOnDesk {get;组; }


//构造函数

public SharedClassExample()

{

BooksOnShelf = new List< string>();

BooksOnDesk = new List< string>();

}


public void SortBooksOnShelf()< br $>
{

BooksOnShelf.Sort();

}


//为什么不是这可能吗?

部分无效RemoveBooksFromShelfAndDesk(字符串bookTitle)

{

BooksOnShef.Remove(bookTitle);

}
}


//在SharedClassExample2.cs内

公共部分类SharedClassExample

{

public void SortBooksOnDesk()

{

BooksOnDesk.Sort();

}


//为什么这不可能?

partial void RemoveBooksFromShelfAndDesk(string bookTitle)

{

BooksOnDesk。删除(bookTitle);

}

}

以上示例生成编译时错误,经过一些r esearch

上线它似乎部分方法不起作用,因为我原来

理解。相反,部分方法是指部分方法。简单地在一个文件中定义

(在上下文中,file表示此类

生活的两个文件中的一个,分为两部分)并且实现是在另一个

文件中提供。


我的一些问题是:如果部分方法的实现

不能跨越跨文件,那又有什么意义呢?例如,在部分类的C#

2.0中,我可以在SharedClassExample1.cs中从

调用SortBooksOnDesk,那么为什么我需要这个新的机制

从声明中分离实现?

So I have a class that spans over two partial classes in code, here''s
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).

// Inside SharedClassExample1.cs
public partial class SharedClassExample
{

public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }

// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}

public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}

// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}
}

// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}

// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}
}
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.

Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?


推荐答案

JDeats写道:
JDeats wrote:

我的一些问题是:如果部分方法

的实现不能跨越文件,那又有什么意义呢?
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point?



重点是允许代码生成器生成仍可由客户端扩展的代码,而无需修改生成的代码:


partial class Generated {

private void DoStuff(){

//这里生成的代码。

AfterDoStuff();

}


partial private void AfterDoStuff();

}

客户端现在可以写出该类的另一半,实现

AfterDoStuff()。如果他们不是,那么就没有方法,而且电话就是

简单地消除了。


这可以通过代表甚至是事件来实现好吧,但是如果你有很多这样的钩子(例如由

LINQ生成的DataContext类提供的那些),那么

这会变得非常集群且效率低下。 />
输入部分方法作为一种简单,轻量级的扩展机制。


基本上,代码生成就是所有部分方法(和部分类)

旨在支持。

The point is to allow code generators to generate code that can still be
extended by clients, without having to modify the generated code:

partial class Generated {
private void DoStuff() {
// Generated code here.
AfterDoStuff();
}

partial private void AfterDoStuff();
}

The client can now write up the other half of the class, implementing
AfterDoStuff(). If they don''t, then there is no method and the call is
simply eliminated.

This could have been implemented with delegates or even events as well, but
if you have a lot of hooks this way (such as the ones provided by
LINQ-generated DataContext classes) this gets pretty clusmy and inefficient.
Enter partial methods as a simple, lightweight extension mechanism.

Basically, code generation is all that partial methods (and partial classes)
are intended to support.


例如,在带有部分类的C#2.0中,我可以拨打电话

来自SharedClassExample1.cs内的SortBooksOnDesk那么为什么我需要这个

从声明中分离实现的新机制呢?
For example, in C# 2.0 with partial classes I could make a call
SortBooksOnDesk from inside SharedClassExample1.cs so why do I need this
new mechanism of seperating the implementation from the declaration?



*你*不要。事实上,除非你正在编写一个代码生成器,否则没有理由打扰部分代码。一点都不它只会让事情变得更难找。

如果你的班级太大而不能部分开始看起来不错,现在是时候给b $ b重写课了。无论是用硬盘还是用纸代码

区域。这仍然比将课程分成多个文件更好。


-

J.

*You* don''t. In fact, unless you''re writing a code generator, there''s no
reason to bother with "partial" at all. It only makes things harder to find.
If your class is so big that "partial" starts to look nice, it''s time to
rewrite the class. Either that or paper over the hard bits with code
regions. That''s still better than splitting up the class over multiple files.

--
J.

8月6日下午2:39,JDeats< Jeremy.De ... @ gmail.comwrote:
On Aug 6, 2:39 pm, JDeats <Jeremy.De...@gmail.comwrote:

所以我有一个跨越的课程在代码中超过两个部分类,这里的'

一个例子(不要读太多,代码是没有实际的

使用,这只是一个简单的我发生混淆的例子。


//在SharedClassExample1.cs内部

公共部分类SharedClassExample

{


public List< stringBooksOnShelf {get;组; }

public List< stringBooksOnDesk {get;组; }


//构造函数

public SharedClassExample()

{

BooksOnShelf = new List< string>();

BooksOnDesk = new List< string>();

}


public void SortBooksOnShelf()< br $>
{

BooksOnShelf.Sort();

}


//为什么不是这可能吗?

部分无效RemoveBooksFromShelfAndDesk(字符串bookTitle)

{

BooksOnShef.Remove(bookTitle);

} $ / $

//在SharedClassExample2.cs内

公共部分类SharedClassExample

{

public void SortBooksOnDesk()

{

BooksOnDesk.Sort();

}


//为什么这不可能?

partial void RemoveBooksFromShe lfAndDesk(string bookTitle)

{

BooksOnDesk.Remove(bookTitle);

}


} $ / $

上面的例子生成一个编译时错误,经过一些研究后,b
在线看来,部分方法不能像我原来那样工作

理解。相反,部分方法是指部分方法。简单地在一个文件中定义

(在上下文中,file表示此类

生活的两个文件中的一个,分为两部分)并且实现是在另一个

文件中提供。


我的一些问题是:如果部分方法的实现

不能跨越跨文件,那又有什么意义呢?例如,在部分类的C#

2.0中,我可以在SharedClassExample1.cs中从

调用SortBooksOnDesk,那么为什么我需要这个新的机制

从声明中分离实施?
So I have a class that spans over two partial classes in code, here''s
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).

// Inside SharedClassExample1.cs
public partial class SharedClassExample
{

public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }

// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}

public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}

// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}

}

// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}

// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}

}

The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.

Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?



IT与部分类不同,在部分类中你是有效地划分实体(在这种情况下是一个类)超过

一个文件。

部分方法更像是如果存在则使用它

如果不忽略调用 ;这种方法。

IIRC的iroginal意图是代码生成框架

(比如LINQ to SQL)你可以生成调用其他方法的方法
$ b可能由用户实现的$ b方法,如果存在这些方法

然后生成调用,如果没有生成调用则因此没有

性能开销发生在编译后的代码

IT''s different than partial classes, in partial classes you are
effectively dividing an entity (in this case a class) among more than
one file.
partial methods are intended to be like more like a "if exist use it
if not ignore the call" kind of approach.
IIRC the iroginal intention was for the code generations framework
(like LINQ to SQL) where you can generate methods that call other
methods that might be implemented by the user, if those method exist
then the call is generated, if not the call is not generate hence no
performance overhead occur in the compiled code


8月6日下午1:56,Ignacio Machin(.NET / C#MVP)

< ignacio.mac ... @ gmail.comwrote:
On Aug 6, 1:56 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:

8月6日下午2:39,JDeats< Jeremy.De ... @ gmail.comwrote:
On Aug 6, 2:39 pm, JDeats <Jeremy.De...@gmail.comwrote:

所以我有一个跨越代码中两个部分类的类,这里是'/ b
一个例子(不要读太多这个,代码是没有实际的使用,这只是一个简单的例子,我的混乱发生在哪里)。
So I have a class that spans over two partial classes in code, here''s
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).


//在SharedClassExample1.cs内

公共部分类SharedClassExample

{
// Inside SharedClassExample1.cs
public partial class SharedClassExample
{


public List< stringBooksOnShelf {get;组; }

public List< stringBooksOnDesk {get;组; }
public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }


//构造函数

public SharedClassExample()

{

BooksOnShelf =新列表< string>();

BooksOnDesk =新列表< string>();

}
// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}


public void SortBooksOnShelf()

{

BooksOnShelf.Sort();

}
public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}


//为什么这不可能?

partial void RemoveBooksFromShelfAndDesk(string bookTitle)

{

BooksOnShef.Remove(bookTitle);

}
// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}


}
}


//在SharedClassExample2.cs内

公共部分类SharedClassExample

{

public void SortBooksOnDesk()

{

BooksOnDesk.Sort();

}
// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}


//为什么这不可能?

partial void RemoveBooksFromShelfAndDesk(string bookTitle)

{

BooksOnDesk.Remove(bookTitle);

}
// why isn''t this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}


}
}


上面的例子生成一个编译时错误,经过一些研究后,

在线看来,部分方法没有像我原来那样工作

理解。相反,部分方法是指部分方法。简单地在一个文件中定义

(在上下文中,file表示此类

生活的两个文件中的一个,分为两部分)并且实现是在另一个

文件中提供。
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.


我的一些问题是:如果部分方法

的实现不能跨越文件,那么什么是关键点?例如,在部分类的C#

2.0中,我可以在SharedClassExample1.cs中从

调用SortBooksOnDesk,那么为什么我需要这个新的机制

从声明中分离实施?
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?



IT与部分类不同,在部分类中你是有效划分实体(在本例中是一个类)超过

一个文件。

部分方法更像是如果存在则使用它

如果不忽略调用 ;这种方法。

IIRC的iroginal意图是代码生成框架

(比如LINQ to SQL)你可以生成调用其他方法的方法
$ b可能由用户实现的$ b方法,如果存在这些方法

然后生成调用,如果没有生成调用则因此没有

性能开销发生在已编译的代码


IT''s different than partial classes, in partial classes you are
effectively dividing an entity (in this case a class) among more than
one file.
partial methods are intended to be like more like a "if exist use it
if not ignore the call" kind of approach.
IIRC the iroginal intention was for the code generations framework
(like LINQ to SQL) where you can generate methods that call other
methods that might be implemented by the user, if those method exist
then the call is generated, if not the call is not generate hence no
performance overhead occur in the compiled code



谢谢!你们都帮助澄清了这一点。根据我对新的

的理解,我认为术语部分类是指是一个用词不当,但除了那个以外,我很满意他们:)

Thanks! You have both helped clear this up. Based on my new
understanding I think the term "partial class" is a misnomer, but
other than that I''m perfectly fine with them :)


这篇关于寻找其他C#程序员关于部分方法的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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