- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我读过 this similar question我不希望出现与 OP 相同的行为,我也不太了解他,但我在派生类中使用了 protected 成员。
在Why is the 'this' keyword required to call an extension method from within the extended class埃里克·利珀特写道:
... If you are in the scenario where you are using an extension methodfor a type within that type then you do have access to the sourcecode. Why are you using an extension method in the first place then?
... Given those two points, the burden no longer falls on the languagedesigner to explain why the feature does not exist. It now falls onyou to explain why it should. Features have enormous costs associatedwith them.
...
所以我将尝试解释为什么我会期望一种行为及其用法示例。
特点:
this
的 protected 成员扩展方法中的对象。this
类型的类中使用该方法对象。this
调用参数对象与 this
可访问的对象相同调用方方法中的关键字。现实生活中的使用场景:
我正在尝试基于 WPFDesigner_XML 创建一个 Visual Studio 自定义编辑器例子。目前我正在尝试通过以下签名在类里面解决问题:
public sealed class EditorPane : WindowPane, IOleComponent, IVsDeferredDocView, IVsLinkedUndoClient
{...}
很多方法都在使用这样的服务:
void RegisterIndependentView(bool subscribe)
{
IVsTextManager textManager = (IVsTextManager)GetService(typeof(SVsTextManager));
if (textManager != null)
{
if (subscribe)
{
textManager.RegisterIndependentView(this, _textBuffer);
}
else
{
textManager.UnregisterIndependentView(this, _textBuffer);
}
}
}
我喜欢专注于真正重要的事情,所以我编写了辅助方法来简化此类方法。例如:
private void RegisterIndependentView(bool subscribe) {
if (with(out IVsTextManager tm)) return;
if (subscribe) tm.RegisterIndependentView(this, _textBuffer);
else tm.UnregisterIndependentView(this, _textBuffer);
}
with
方法如下所示:
private bool with<T>(out T si) {
si = (T)GetService(getServiceQueryType<T>());
return si == null ? true : false;
}
然后我放置了getServiceQueryType<T>()
在静态类中:
public static class VSServiceQueryHelper {
public static Type getServiceQueryType<T>() {
var t = typeof(T);
if (!serviceQueryTypesMap.ContainsKey(t)) throw new Exception($@"No query type was mapped in ""{nameof(serviceQueryTypesMap)}"" for the ""{t.FullName}"" interface.");
return serviceQueryTypesMap[t];
}
private static Dictionary<Type, Type> serviceQueryTypesMap = new Dictionary<Type, Type>() {
{ typeof(IVsUIShellOpenDocument), typeof(SVsUIShellOpenDocument) },
{ typeof(IVsWindowFrame), typeof(SVsWindowFrame) },
{ typeof(IVsResourceManager), typeof(SVsResourceManager) },
{ typeof(IVsRunningDocumentTable), typeof(SVsRunningDocumentTable) },
{ typeof(IMenuCommandService), typeof(IMenuCommandService) },
{ typeof(IVsTextManager), typeof(SVsTextManager) },
};
}
这很好用,但我还想将 with
里面的方法VSServiceQueryHelper
作为扩展,所以任何时候我都会扩展 WindowsPane
我可以把using static com.audionysos.vsix.utils.VSServiceQueryHelper;
在顶部并使用 with
已经实现的方法。
问题:
我做不到with
方法扩展,因为 GetService
它使用的方法是 WindowsPane
的 protected 成员这是我类(class)的基本类型。所以现在我需要放置 with
在扩展 WindowPane
的每个类中实现这打破了永不重复自己的规则:/
最佳答案
一个简单的解决方案是创建一个包含 With 方法的基类。
如果这太繁琐,那么您也可以使用反射来实现,从扩展方法中调用 GetService 方法。事实上,我们可以为它创建一个委托(delegate),以确保多次调用 With 的开销最小。
internal static class WindowPaneExtensions
{
private static readonly Func<WindowPane, Type, object> WindowPaneGetService = CreateWindowPaneGetService();
public static bool With<T>(this WindowPane pane, out T service)
{
service = (T)WindowPaneGetService(pane, GetServiceQueryType<T>());
return service != null;
}
private static Func<WindowPane, Type, object> CreateWindowPaneGetService()
{
var method = typeof(WindowPane).GetMethod("GetService", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(Type) }, null);
var del = (Func<WindowPane, Type, object>)method.CreateDelegate(typeof(Func<WindowPane, Type, object>));
return del;
}
}
我认为您允许某些扩展方法访问 protected 成员的提议是行不通的。例如,不允许以下内容:
public class MyPane : WindowPane
{
public static void Test(WindowPane p)
{
var service = p.GetService(typeof(Service));
// etc.
}
}
但是你问,“不是允许从派生类访问基类成员吗?”不,这实际上不是规则。规则是您只能通过对派生类的引用从派生类访问基类成员,而不能直接从任何基类引用访问。有关 this here 的更多详细信息.您的建议相当于允许这种事情用于更大的类方法(即其他一些库作者声明为扩展方法的方法)。 Eric Lippert 过去也写过关于这个问题的文章(here 和 here)。由于跨层次结构调用被 CLR 阻止,我不希望像这个提议那样的东西很快得到实现。
关于c# - 为从 `this` 类型派生的类型提供扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797046/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!