我在玩ScriptCS(這太棒了!)但是我找不到如何在.csx腳本文件中定義擴展方法。如何在scriptcs csx腳本中定義擴展方法
拿這個例子:
using System.IO;
public static class Extensions
{
public static string Remove(this string source, params string[] toRemove)
{
foreach(var r in toRemove)
{
source = source.Replace(r,"");
}
return source;
}
}
string[] entries =
Directory
.GetFiles(
@"C:\Users\blah\blah",
"*.mp4",
SearchOption.AllDirectories)
.Select(p => p.Remove("Users"))
.ToArray();
foreach(var e in entries)
{
Console.WriteLine(e);
}
這產生了錯誤:
error CS1109: Extension methods must be defined in a top level static class; Extensions is a nested class
我猜ScriptCS包裹CSX在一些類,這是造成擴展嵌套,沒有任何繞過這個方法?
這是Roslyn的限制。請參閱:http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/7c0a18eb-d37a-40e2-a26c-edb59b8f5cf3/。所以目前你不能在'.csx'文件中定義擴展方法。你需要把它們放在一個dll中,並引用該dll。 – nemesv
@nemesv this look answer-worthy =)謝謝 – TJB