A senior member here gave me this code:
这里的高级成员给了我这个代码:
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " ..";
}
He said to use it as an extension method. But where do I put this method? It looks like it adds something to .Net
他说要用它作为扩展方法。但是我在哪里放这种方法呢?看起来它为.Net增加了一些东西
4 个解决方案
#1
31
Consider a class named StringExtensions
like so:
考虑一个名为StringExtensions的类,如下所示:
static class StringExtensions {
public static string Truncate(this string value, int maxChars) {
return value.Length <= maxChars ?
value :
value.Substring(0, maxChars) + " ..";
}
}
Be sure that whatever namespace you put this class in, you include a using
declaration for that namespace.
确保无论您将此类放入哪个命名空间,都要包含该命名空间的using声明。
Thus, for a full example:
因此,举一个完整的例子:
StringExtensions.cs
:
StringExtensions.cs:
namespace My.Extensions {
static class StringExtensions {
public static string Truncate(this string value, int maxChars) {
return value.Length <= maxChars ?
value :
value.Substring(0, maxChars) + " ..";
}
}
}
Program.cs
:
Program.cs中:
using System;
using My.Extensions;
namespace My.Program {
static class Program {
static void Main(string[] args) {
string s = "Hello, World";
string t = s.Truncate(5);
Console.WriteLine(s);
Console.WriteLine(t);
}
}
}
By the way, you are not adding it to .NET. You are not even adding a new method to the class String
. Rather, it's a compiler trick that makes static methods living in static classes with their first parameter declared as this *TypeName* *valueParameter*
where *TypeName*
is the name of a type, and *valueParameter*
is the name of the parameter can be made to appear as an instance method on instances of the type with type name *TypeName*
. That is
顺便说一下,你没有将它添加到.NET。您甚至没有向String类添加新方法。相反,它是一个编译器技巧,使静态方法生成在静态类中,其第一个参数声明为此* TypeName * * valueParameter *其中* TypeName *是类型的名称,而* valueParameter *是参数的名称可以是在类型名称为* TypeName *的类型的实例上显示为实例方法。那是
string t = s.Truncate(5);
is translated by the compiler into
由编译器翻译成
string t = StringExtensions.Truncate(s, 5);
#2
1
Put it in a static class, and use using
on its namespace.
将它放在一个静态类中,并在其命名空间中使用using。
e.g.
例如
namespace Foo
{
static class Extensions
{
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ?
value : value.Substring(0, maxChars) + " ..";
}
}
}
And then in a different file:
然后在另一个文件中:
using Foo; //Don't forget this!
class Tester
{
static void Test()
{
Console.WriteLine("123456".Truncate(3));
}
}
#3
0
Yes, use a static class. I organize in a separate project that I can use across solutions. I also organize in separate files grouped by what I'm extending such as strings, enums, io, datetime, etc
是的,使用静态类。我组织了一个单独的项目,我可以跨解决方案使用。我还根据我正在扩展的内容(例如字符串,枚举,io,日期时间等)组织单独的文件
#4
0
In addition to other answers: yes, it's a kind of extending .NET. More strictly speaking, extending already compiled classes. You can "add" methods to the classes which are not accessible for your modification. From the internal point of view, it's just a syntactic sugar: your methods cannot be seen by reflection. But for the users of your extension, it looks as if the methods were really added to the target class (well, with some distinctions).
除了其他答案:是的,它是一种扩展的.NET。更严格地说,扩展已编译的类。您可以向无法修改的类“添加”方法。从内部的角度来看,它只是一种语法糖:你的方法不能通过反思看出来。但是对于扩展的用户来说,看起来好像这些方法确实被添加到了目标类中(好吧,有一些区别)。
The possibility to influence in some way the code that is already written is an essential part of object-oriented approach. In almost any OO language you can derive from some existing class and add some functionality to it this way (although this is not the preferred way for code reusing). In Objective C, you can modify existing classes using categories at compile time. In JavaScript, you can modify them even at runtime, using prototype.
以某种方式影响已经编写的代码的可能性是面向对象方法的重要部分。在几乎任何OO语言中,您都可以从一些现有类派生并以这种方式添加一些功能(尽管这不是代码重用的首选方式)。在Objective C中,您可以在编译时使用类别修改现有类。在JavaScript中,您甚至可以在运行时使用原型修改它们。
As C# is not such a dynamic language as JavaScript is, modifying the existing classes is available in a somewhat limited form of extension methods.
由于C#不是像JavaScript那样的动态语言,因此可以通过某种有限形式的扩展方法来修改现有类。
#1
31
Consider a class named StringExtensions
like so:
考虑一个名为StringExtensions的类,如下所示:
static class StringExtensions {
public static string Truncate(this string value, int maxChars) {
return value.Length <= maxChars ?
value :
value.Substring(0, maxChars) + " ..";
}
}
Be sure that whatever namespace you put this class in, you include a using
declaration for that namespace.
确保无论您将此类放入哪个命名空间,都要包含该命名空间的using声明。
Thus, for a full example:
因此,举一个完整的例子:
StringExtensions.cs
:
StringExtensions.cs:
namespace My.Extensions {
static class StringExtensions {
public static string Truncate(this string value, int maxChars) {
return value.Length <= maxChars ?
value :
value.Substring(0, maxChars) + " ..";
}
}
}
Program.cs
:
Program.cs中:
using System;
using My.Extensions;
namespace My.Program {
static class Program {
static void Main(string[] args) {
string s = "Hello, World";
string t = s.Truncate(5);
Console.WriteLine(s);
Console.WriteLine(t);
}
}
}
By the way, you are not adding it to .NET. You are not even adding a new method to the class String
. Rather, it's a compiler trick that makes static methods living in static classes with their first parameter declared as this *TypeName* *valueParameter*
where *TypeName*
is the name of a type, and *valueParameter*
is the name of the parameter can be made to appear as an instance method on instances of the type with type name *TypeName*
. That is
顺便说一下,你没有将它添加到.NET。您甚至没有向String类添加新方法。相反,它是一个编译器技巧,使静态方法生成在静态类中,其第一个参数声明为此* TypeName * * valueParameter *其中* TypeName *是类型的名称,而* valueParameter *是参数的名称可以是在类型名称为* TypeName *的类型的实例上显示为实例方法。那是
string t = s.Truncate(5);
is translated by the compiler into
由编译器翻译成
string t = StringExtensions.Truncate(s, 5);
#2
1
Put it in a static class, and use using
on its namespace.
将它放在一个静态类中,并在其命名空间中使用using。
e.g.
例如
namespace Foo
{
static class Extensions
{
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ?
value : value.Substring(0, maxChars) + " ..";
}
}
}
And then in a different file:
然后在另一个文件中:
using Foo; //Don't forget this!
class Tester
{
static void Test()
{
Console.WriteLine("123456".Truncate(3));
}
}
#3
0
Yes, use a static class. I organize in a separate project that I can use across solutions. I also organize in separate files grouped by what I'm extending such as strings, enums, io, datetime, etc
是的,使用静态类。我组织了一个单独的项目,我可以跨解决方案使用。我还根据我正在扩展的内容(例如字符串,枚举,io,日期时间等)组织单独的文件
#4
0
In addition to other answers: yes, it's a kind of extending .NET. More strictly speaking, extending already compiled classes. You can "add" methods to the classes which are not accessible for your modification. From the internal point of view, it's just a syntactic sugar: your methods cannot be seen by reflection. But for the users of your extension, it looks as if the methods were really added to the target class (well, with some distinctions).
除了其他答案:是的,它是一种扩展的.NET。更严格地说,扩展已编译的类。您可以向无法修改的类“添加”方法。从内部的角度来看,它只是一种语法糖:你的方法不能通过反思看出来。但是对于扩展的用户来说,看起来好像这些方法确实被添加到了目标类中(好吧,有一些区别)。
The possibility to influence in some way the code that is already written is an essential part of object-oriented approach. In almost any OO language you can derive from some existing class and add some functionality to it this way (although this is not the preferred way for code reusing). In Objective C, you can modify existing classes using categories at compile time. In JavaScript, you can modify them even at runtime, using prototype.
以某种方式影响已经编写的代码的可能性是面向对象方法的重要部分。在几乎任何OO语言中,您都可以从一些现有类派生并以这种方式添加一些功能(尽管这不是代码重用的首选方式)。在Objective C中,您可以在编译时使用类别修改现有类。在JavaScript中,您甚至可以在运行时使用原型修改它们。
As C# is not such a dynamic language as JavaScript is, modifying the existing classes is available in a somewhat limited form of extension methods.
由于C#不是像JavaScript那样的动态语言,因此可以通过某种有限形式的扩展方法来修改现有类。