Tag: extension methods

“this”关键字在方法参数中的含义是什么?

namespace System.Web.Mvc.Html { // Summary: // Represents support for HTML in an application. public static class FormExtensions { public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName); … } } 我注意到在BeginForm方法的第一个参数前面的’this’对象似乎不被接受为参数。 看起来在真正的BeginForm方法中的function如下: BeginForm(string actionName, string controllerName); 省略第一个参数。 但它实际上以隐藏的方式以某种方式接收第一个参数。 能告诉我这个结构是如何工作的吗? 我实际上在探索MVC 4互联网示例。 谢谢。

无法等待异步扩展方法

情况非常简单 – 我编写了一个扩展方法,并将其与返回类型Task进行异步。 但是当我尝试使用await调用它时,编译器会抛出一个错误,表明扩展方法根本不被识别为异步。 这是我的代码: public static async Task<NodeReference> CreateWithLabel(this GraphClient client, T source, String label) where T: class { var node = client.Create(source); var url = string.Format(ConfigurationManager.AppSettings[configKey] + “/node/{0}/labels”, node.Id); var serializedLabel = string.Empty; using (var tempStream = new MemoryStream()) { new DataContractJsonSerializer(label.GetType()).WriteObject(tempStream, label); serializedLabel = Encoding.Default.GetString(tempStream.ToArray()); } var bytes = Encoding.Default.GetBytes(serializedLabel); using (var […]

扩展C#Coalesce运算符

在我解释我想要做什么之前,如果你看下面的代码,你会明白它应该做什么吗? (更新 – 见下文) Console.WriteLine( Coalesce.UntilNull(getSomeFoo(), f => f.Value) ?? “default value”); C#已经有一个null-coalescing运算符,它在简单对象上运行得很好,但如果你需要访问该对象的一个​​成员则无效。 例如 Console.WriteLine(getSomeString()??”default”); 效果很好,但它不会帮助你: public class Foo { public Foo(string value) { Value=value; } public string Value { get; private set; } } // this will obviously fail if null was returned Console.WriteLine(getSomeFoo().Value??”default”); // this was the intention Foo foo=getSomeFoo(); Console.WriteLine(foo!=null?foo.Value:”default”); 由于这是我经常遇到的事情,我想到使用扩展方法(旧版本) : […]

C#扩展方法是否允许通过引用传递参数?

是否真的不可能在C#中创建一个扩展方法,其中实例作为引用传递? 这是一个示例VB.NET控制台应用程序: Imports System.Runtime.CompilerServices Module Module1 Sub Main() Dim workDays As Weekdays workDays.Add(Weekdays.Monday) workDays.Add(Weekdays.Tuesday) Console.WriteLine(“Tuesday is a workday: {0}”, _ CBool(workDays And Weekdays.Tuesday)) Console.ReadKey() End Sub End Module _ Public Enum Weekdays Monday = 1 Tuesday = 2 Wednesday = 4 Thursday = 8 Friday = 16 Saturday = 32 Sunday = 64 End Enum […]

创建以“对象”类型操作的扩展方法是否会影响性能?

我有一组扩展方法,我经常用于各种UI任务。 我通常将它们定义为运行类型object ,即使在它们内部我通常将它们转换为字符串类型。 public static string FormatSomething(this object o) { if( o != null ) { string s = o.ToString(); /// do the work and return something. } // return something else or empty string. } 我使用类型object而不是string主要原因是为了保存自己在UI中不必执行当我可以执行代替。 那么,从性能的角度来看,在object上创建所有扩展方法是否正常,或者我应该根据扩展方法的作用将它们转换为string (或相关)类型?

导致“扩展方法无法动态调度”的原因在这里?

编译错误 ‘System.Data.SqlClient.SqlConnection’没有名为’Query’的适用方法,但似乎有一个名称的扩展方法。 无法动态分派扩展方法。 考虑转换动态参数或调用扩展方法而不使用扩展方法语法。 现在,我知道如何解决这个问题,但我正在努力更好地理解错误本身。 我正在上课,以利用Dapper。 最后,我将提供一些更多自定义function,以使我们的数据访问类型更加简化。 特别是在追踪和东西的建设。 但是,现在它就像这样简单: public class Connection : IDisposable { private SqlConnection _connection; public Connection() { var connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings[“ConnectionString”]); _connection = new SqlConnection(connectionString); _connection.Open(); } public void Dispose() { _connection.Close(); _connection.Dispose(); } public IEnumerable Query(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? […]

我可以在.NET 2.0或3.0中使用扩展方法和LINQ吗?

当我尝试使用.NET 2.0或3.0运行时添加扩展方法时,我收到错误: 无法定义新的扩展方法,因为无法找到编译器所需的类型“System.Runtime.CompilerServices.ExtensionAttribute”。 您是否缺少对System.Core.dll的引用? 但是当我尝试将它添加到项目中时,我无法在可用引用列表中找到System.Core。 我需要做什么才能在我的项目中使用扩展方法和LINQ on?

为什么不包含有什么影响?

我正在做以下LINQ查询,但它不会返回导航属性Person填充,我得到null 。 public IEnumerable GetSharePeopeByCarId(int carId) { return from q in _context.Cars join s in _context.Shares on q.CarId equals s.Car.CarId join p in _context.SharePeople.Include(p => p.Person) on s.ShareId equals p.ShareId where q.CarId == carId select p; } 我不知道为什么,因为当我使用像_context.SharePeople.Include(p => p.Person)这样的常规扩展方法时,它可以工作。