C# 扩展方法不被识别,有什么问题吗?
我的扩展方法无法识别,有什么问题吗?在调用前加上C# 扩展方法不被识别,有什么问题吗?,c#,C#,我的扩展方法无法识别,有什么问题吗?在调用前加上this: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Web; using System.Web.Mvc; namespace MvcMusicStore.Controllers { public class StoreController :
this
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;
namespace MvcMusicStore.Controllers
{
public class StoreController : Controller
{
//
// GET: /Store/
public string Index()
{
// ERROR call extension method here
return GetMemberName();
}
}
public static class Utilities
{
public static string GetMemberName(this Controller caller, [CallerMemberName] string memberName = "")
{
return caller.GetType().FullName + "." + memberName;
}
}
}
哦,我的鬼,我们必须明确指定
这个:-)好球。。。我看不出OP试图做什么。你知道为什么我们必须显式指定this
?因为扩展方法只能处理实例,并且指向实例…@Kai:但为什么必须显式指定它?:-)我们在被扩展的类的方法中调用它,因此这个应该可以忽略不计。
public class StoreController : Controller
{
//
// GET: /Store/
public string Index()
{
return this.GetMemberName();
}
}