LINQPad [扩展]方法 [英] LINQPad [extension] methods

查看:156
本文介绍了LINQPad [扩展]方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有LINQPad扩展方法和方法的完整列表,如

  .Dump()

SubmitChanges()


解决方案




LINQPad定义了两个扩展方法(在LINQPad.Extensions中),即 Dump() / code>和 Disassemble() Dump()使用LINQPad的输出格式化程序写入输出窗口,并重载以指定一个标题:

  typeof(int).Assembly.Dump(); 
typeof(int).Assembly.Dump(mscorlib);

您还可以指定最大递归深度来覆盖默认的5个级别:

  typeof(int).Assembly.Dump(1); //转储一级深
typeof(int).Assembly.Dump(7); // Dump 7 levels deep
typeof(int).Assembly.Dump(mscorlib,7); //转储7级深度,标题为

反汇编()将任何方法拆分为 IL ,返回字符串中的输出:

  typeof(Uri ).GetMethod(GetHashCode)。Disassemble()。Dump(); 

除了这两种扩展方法,LINQPad.Util中还有一些有用的静态方法。这些记录在自动完成中,包括:




  • Cmd - 执行shell命令或外部程序

  • CreateXhtmlWriter - 创建一个使用LINQPad的Dump()格式化程序的文本编写器

  • SqlOutputWriter - 返回写入SQL输出窗口的文本写入器

  • GetMyQueries GetSamples - 返回表示保存的查询/样本的对象集合(例如,使用编辑|搜索全部执行搜索)

  • 突出显示 - 包装对象,以便在Dumped
  • $时会以黄色突出显示b $ b
  • Horizo​​ntalRun - 让您在同一行转储一系列对象



LINQPad还提供了HyperLinq类。这有两个目的:第一个是显示普通超链接:

  new Hyperlinq(www。 linqpad.net)转储(); 
new Hyperlinq(www.linqpad.net,网站)。Dump();
new Hyperlinq(mailto:user@domain.com,Email)。Dump();

您可以将其与 Util.Horizo​​ntalRun

  Util.Horizo​​ntalRun(true,
退房,
新的Hyperlinq(http://stackoverflow.com,本站),
用于编程问题的答案。)Dump();

结果:


查看本网站以获取编程问题的答案。


HyperLinq的第二个目的是动态构建查询:

  //动态构建简单的表达式:
new Hyperlinq(QueryLanguage.Expression,123 * 234)。Dump();

//动态构建查询:
new Hyperlinq(QueryLanguage.Expression,@from c in Customers
where c.Name.Length> 3
select c 。名称,点击运行!)。Dump();

您还可以在LINQPad中编写自己的扩展方法。转到我的查询,然后单击名为我的扩展的查询。任何在这里定义的类型/方法都可以访问所有查询:

  void Main()
{
hello.Pascal()。Dump();
}

public static class MyExtensions
{
public static string Pascal(this string s)
{
return char.ToLower(s [0])+ s.Substring(1);
}
}

在4.46(.02)新课程和方法已经介绍




  • DumpContainer(class)

  • OnDemand(扩展方法)

  • Util.ProgressBar(class)



此外,Hyperlinq类现在支持 Action 代理,当您点击链接时,将被调用,允许您对代码进行反应,而不仅仅是链接到外部网页。



DumpContainer 是一个类,可以在输出窗口中添加一个可以替换其内容的块。



strong>注意记住在适当的位置 .Dump() $ code DumpContainer 本身。 p>

要使用:

  var dc =新的DumpContainer() ; 
dc.Content =Test;
//进一步在代码中
dc.Content =另一个测试;

OnDemand 是一种扩展方法,不会将其参数的内容输出到输出窗口,而是添加一个可点击的链接,当单击时,链接将替换参数的 .Dump() ed链接。这对于有时需要的数据结构来说是非常有用的,这些数据结构是昂贵的或占用了大量空间。



注意记住 .Dump()在适当的位置调用 OnDemand 的结果。



使用它:

  Customers.OnDemand(Customers)。 //描述是可选的

Util.ProgressBar 是一个类,可以在输出窗口中显示图形进度条,可以随着代码的移动而改变。



注意记住



要使用它:



  var pb = new Util.ProgressBar(Analyzing data); 
pb.Dump(); (int index = 0; index< = 100; index ++)

{
pb.Percent = index;
Thread.Sleep(100);
}


Does anyone have a complete list of LINQPad extension methods and methods, such as

.Dump()

SubmitChanges()

解决方案

LINQPad defines two extension methods (in LINQPad.Extensions), namely Dump() and Disassemble(). Dump() writes to the output window using LINQPad's output formatter and is overloaded to let you specify a heading:

typeof (int).Assembly.Dump ();
typeof (int).Assembly.Dump ("mscorlib");

You can also specify a maximum recursion depth to override the default of 5 levels:

typeof (int).Assembly.Dump (1);              // Dump just one level deep
typeof (int).Assembly.Dump (7);              // Dump 7 levels deep
typeof (int).Assembly.Dump ("mscorlib", 7);  // Dump 7 levels deep with heading

Disassemble() disassembles any method to IL, returning the output in a string:

typeof (Uri).GetMethod ("GetHashCode").Disassemble().Dump();

In addition to those two extension methods, there are some useful static methods in LINQPad.Util. These are documented in autocompletion, and include:

  • Cmd - executes a shell command or external program
  • CreateXhtmlWriter - creates a text writer that uses LINQPad's Dump() formatter
  • SqlOutputWriter - returns the text writer that writes to the SQL output window
  • GetMyQueries, GetSamples - returns a collection of objects representing your saved queries / samples (for an example, execute a search using Edit | Search All)
  • Highlight - wraps an object so that it will highlight in yellow when Dumped
  • HorizontalRun - lets you Dump a series of objects on the same line

LINQPad also provides the HyperLinq class. This has two purposes: the first is to display ordinary hyperlinks:

new Hyperlinq ("www.linqpad.net").Dump();
new Hyperlinq ("www.linqpad.net", "Web site").Dump();
new Hyperlinq ("mailto:user@domain.com", "Email").Dump();

You can combine this with Util.HorizontalRun:

Util.HorizontalRun (true,
  "Check out",
   new Hyperlinq ("http://stackoverflow.com", "this site"),
  "for answers to programming questions.").Dump();

Result:

Check out this site for answers to programming questions.

The second purpose of HyperLinq is to dynamically build queries:

// Dynamically build simple expression:
new Hyperlinq (QueryLanguage.Expression, "123 * 234").Dump();

// Dynamically build query:
new Hyperlinq (QueryLanguage.Expression, @"from c in Customers
where c.Name.Length > 3
select c.Name", "Click to run!").Dump();

You can also write your own extension methods in LINQPad. Go to 'My Queries' and click the query called 'My Extensions'. Any types/methods that define here are accessible to all queries:

void Main()
{
  "hello".Pascal().Dump();  
}

public static class MyExtensions
{
  public static string Pascal (this string s)
  {
    return char.ToLower (s[0]) + s.Substring(1);
  }
}

In 4.46(.02) new classes and methods have been introduced:

  • DumpContainer (class)
  • OnDemand (extension method)
  • Util.ProgressBar (class)

Additionally, the Hyperlinq class now supports an Action delegate that will be called when you click the link, allowing you to react to it in code and not just link to external webpages.

DumpContainer is a class that adds a block into the output window that can have its contents replaced.

NOTE! Remember to .Dump() the DumpContainer itself in the appropriate spot.

To use:

var dc = new DumpContainer();
dc.Content = "Test";
// further down in the code
dc.Content = "Another test";

OnDemand is an extension method that will not output the contents of its parameter to the output window, but instead add a clickable link, that when clicked will replace the link with the .Dump()ed contents of the parameter. This is great for sometimes-needed data structures that is costly or takes up a lot of space.

NOTE! Remember to .Dump() the results of calling OnDemand in the appropriate spot.

To use it:

Customers.OnDemand("Customers").Dump(); // description is optional

Util.ProgressBar is a class that can show a graphical progressbar inside the output window, that can be changed as the code moves on.

NOTE! Remember to .Dump() the Util.ProgressBar object in the appropriate spot.

To use it:

var pb = new Util.ProgressBar("Analyzing data");
pb.Dump();
for (int index = 0; index <= 100; index++)
{
    pb.Percent = index;
    Thread.Sleep(100);
}

这篇关于LINQPad [扩展]方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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