gpt4 book ai didi

c# - 为从 `this` 类型派生的类型提供扩展方法

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:14 24 4
gpt4 key购买 nike

我读过 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 成员扩展方法中的对象。
  • 当在扩展方法中使用 protected 成员时,您只能在派生自 this 类型的类中使用该方法对象。
  • protected 扩展方法只能用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 过去也写过关于这个问题的文章(herehere)。由于跨层次结构调用被 CLR 阻止,我不希望像这个提议​​那样的东西很快得到实现。

关于c# - 为从 `this` 类型派生的类型提供扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com