我正在尝试使用扩展方法向C#StringBuilder类添加一个operater重载.具体来说,给定StringBuilder sb,我想sb += "text"成为等同于sb.Append("text").
以下是为以下内容创建扩展方法的语法StringBuilder:
public static class sbExtensions
{
public static StringBuilder blah(this StringBuilder sb)
{
return sb;
}
}
Run Code Online (Sandbox Code Playgroud)
它成功地将blah扩展方法添加到了StringBuilder.
不幸的是,运算符重载似乎不起作用:
public static class sbExtensions
{
public static StringBuilder operator +(this StringBuilder sb, string s)
{
return sb.Append(s);
}
}
Run Code Online (Sandbox Code Playgroud)
除其他问题外,this在此上下文中不允许使用关键字.
是否可以通过扩展方法添加运算符重载?如果是这样,那么正确的方法是什么?
Dunno如果在PR或Beta中发生这种情况,但如果我创建了一个扩展方法HtmlHelper,则在Razor驱动的页面中无法识别:
namespace SomeNamespace.Extensions {
public static class HtmlExtensions {
public static string Foo(this HtmlHelper html) {
return "Foo";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我把它添加到以下<Namespaces>部分Web.config:
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<!-- snip -->
<add namespace="SomeNamespace.Extensions"/>
</namespaces>
</pages>
Run Code Online (Sandbox Code Playgroud)
但是在尝试查看页面时会抛出编译错误:
@Html.Foo()
Run Code Online (Sandbox Code Playgroud)
如果我使用WebForms重新创建页面,它可以正常工作.这是怎么回事?
解决方法
如果我@using SomeNamespace.Extensions在我的Razor视图中包含它,那么它可以工作,但我更愿意将它包含在内Web.config
我希望在C#中使用扩展方法实现对象列表中的功能.
像这样的东西:
List<DataObject> list;
// ... List initialization.
list.getData(id);
Run Code Online (Sandbox Code Playgroud)
我如何用Java做到这一点?
我有一个预先存在的界面......
public interface ISomeInterface
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
并且我使用mixin扩展了这个表面...
public static class SomeInterfaceExtensions
{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个叫这个我要测试的课程...
public class Caller
{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
和测试,我想模拟界面并验证对扩展方法的调用...
[Test]
public void Main_BasicCall_CallsAnotherMethod()
{
// Arrange
var someInterfaceMock = new Mock<ISomeInterface>();
someInterfaceMock.Setup(x => x.AnotherMethod()).Verifiable();
var caller = new Caller(someInterfaceMock.Object);
// Act
caller.Main();
// Assert
someInterfaceMock.Verify();
}
Run Code Online (Sandbox Code Playgroud)
然而,运行此测试会产生异常......
System.ArgumentException: …Run Code Online (Sandbox Code Playgroud) 我正在尝试进行一些数据转换.不幸的是,大部分数据都是字符串,它应该是int或double等等......
所以我得到的是:
double? amount = Convert.ToDouble(strAmount);
Run Code Online (Sandbox Code Playgroud)
这种方法的问题是如果strAmount是空的,如果它是空的我希望它等于null,所以当我将它添加到数据库时,该列将为null.所以我最后写了这个:
double? amount = null;
if(strAmount.Trim().Length>0)
{
amount = Convert.ToDouble(strAmount);
}
Run Code Online (Sandbox Code Playgroud)
现在这个工作正常,但我现在有五行代码而不是一行代码.这使得事情变得更难以阅读,特别是当我有大量的列要转换时.
我以为我会使用字符串类和泛型的扩展来传入类型,这是因为它可能是double,或int或long.所以我尝试了这个:
public static class GenericExtension
{
public static Nullable<T> ConvertToNullable<T>(this string s, T type) where T: struct
{
if (s.Trim().Length > 0)
{
return (Nullable<T>)s;
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:无法将类型'string'转换为'T?'
有没有解决的办法?我不太熟悉使用泛型创建方法.
当我尝试在Kotlin中编写相当于try-with-resources的代码时,它对我不起作用.
我尝试了以下不同的变体:
try (writer = OutputStreamWriter(r.getOutputStream())) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但都不起作用.
有谁知道应该使用什么?显然Kotlin语法没有这种结构的定义,但可能是我遗漏了一些东西.它定义了try块的语法,如下所示:
try : "try" block catchBlock* finallyBlock?;
Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串,例如,
string snip = "</li></ul>";
Run Code Online (Sandbox Code Playgroud)
我想基本上写多次,取决于一些整数值.
string snip = "</li></ul>";
int multiplier = 2;
// TODO: magic code to do this
// snip * multiplier = "</li></ul></li></ul>";
Run Code Online (Sandbox Code Playgroud)
编辑:我知道我可以轻松编写自己的函数来实现这一点,我只是想知道是否有一些我不知道的奇怪的字符串运算符
是否可以将扩展方法应用于接口?(C#问题)
例如,这是为了实现以下目标:
创建ITopology界面
为此接口创建扩展方法(例如public static int CountNodes(this ITopology topologyIf))
然后在创建实现ITopology的类(例如MyGraph)时,它会自动拥有Count Nodes扩展.
这样,实现接口的类就不必具有与扩展方法中定义的类对齐的set类名.
我想在gorilla/mux路由和路由器类型上添加一个方便的util方法:
package util
import(
"net/http"
"github.com/0xor1/gorillaseed/src/server/lib/mux"
)
func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{
return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}
func (r *mux.Router) Subroute(tpl string, h http.Handler) *mux.Route{
return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}
Run Code Online (Sandbox Code Playgroud)
但是编译器通知我
无法在非本地类型mux.Router上定义新方法
那我怎么做到这一点?我是否创建了一个具有匿名mux.Route和mux.Router字段的新结构类型?或者是其他东西?