如何在 C#.Net 中创建原型方法(如 JavaScript)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create Prototype Methods (like JavaScript) in C#.Net?
提问by GateKiller
How is it possible to make prototype methods in C#.Net?
如何在 C#.Net 中创建原型方法?
In JavaScript, I can do the following to create a trim method for the string object:
在 JavaScript 中,我可以执行以下操作来为字符串对象创建一个修剪方法:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
How can I go about doing this in C#.Net?
我怎样才能在 C#.Net 中做到这一点?
采纳答案by Lasse V. Karlsen
You can't dynamically add methods to existing objects or classes in .NET, except by changing the source for that class.
您不能向 .NET 中的现有对象或类动态添加方法,除非更改该类的源。
You can, however, in C# 3.0, use extension methods, which looklike new methods, but are compile-time magic.
但是,您可以在 C# 3.0 中使用扩展方法,这些方法看起来像新方法,但却是编译时的魔法。
To do this for your code:
要为您的代码执行此操作:
public static class StringExtensions
{
public static String trim(this String s)
{
return s.Trim();
}
}
To use it:
要使用它:
String s = " Test ";
s = s.trim();
This looks like a new method, but will compile the exact same way as this code:
这看起来像一个新方法,但编译方式与此代码完全相同:
String s = " Test ";
s = StringExtensions.trim(s);
What exactly are you trying to accomplish? Perhaps there are better ways of doing what you want?
你到底想完成什么?也许有更好的方法来做你想做的事?
回答by David Wengier
You need to create an extension method, which requires .NET 3.5. The method needs to be static, in a static class. The first parameter of the method needs to be prefixed with "this" in the signature.
您需要创建一个需要 .NET 3.5 的扩展方法。该方法需要是静态的,在静态类中。该方法的第一个参数需要在签名中以“this”为前缀。
public static string MyMethod(this string input)
{
// do things
}
You can then call it like
然后你可以这样称呼它
"asdfas".MyMethod();
回答by Andrew Peters
Using the 3.5 compiler you can use an Extension Method:
使用 3.5 编译器,您可以使用扩展方法:
public static void Trim(this string s)
{
// implementation
}
You can use this on a CLR 2.0 targeted project (3.5 compiler) by including this hack:
您可以通过包含此 hack 将其用于 CLR 2.0 目标项目(3.5 编译器):
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}
回答by Matt Hamilton
It sounds like you're talking about C#'s Extension Methods. You add functionality to existing classes by inserting the "this" keyword before the first parameter. The method has to be a static method in a static class. Strings in .NET already have a "Trim" method, so I'll use another example.
听起来您在谈论 C# 的扩展方法。您可以通过在第一个参数之前插入“this”关键字来向现有类添加功能。该方法必须是静态类中的静态方法。.NET 中的字符串已经有一个“Trim”方法,所以我将使用另一个例子。
public static class MyStringEtensions
{
public static bool ContainsMabster(this string s)
{
return s.Contains("Mabster");
}
}
So now every string has a tremendously useful ContainsMabster method, which I can use like this:
所以现在每个字符串都有一个非常有用的 ContainsMabster 方法,我可以这样使用:
if ("Why hello there, Mabster!".ContainsMabster()) { /* ... */ }
Note that you can also add extension methods to interfaces (eg IList), which means that any class implementing that interface will also pick up that new method.
请注意,您还可以向接口添加扩展方法(例如 IList),这意味着实现该接口的任何类也将采用该新方法。
Any extra parameters you declare in the extension method (after the first "this" parameter) are treated as normal parameters.
您在扩展方法中声明的任何额外参数(在第一个“this”参数之后)都被视为普通参数。