VisualStudio 智能感知和 Lambda。我怎样才能让它不选择实际类型?

发布于 2024-11-25 14:01:56 字数 1283 浏览 8 评论 0原文

我正在开发一个实用程序类,并且定义了我的方法之一:

void SomeMethod(Action<T> a);

尴尬的是,在 Visual Studio 2010 中,当您开始键入时:

someClass.SomeMethod(x ...

当您按 [x] 键,然后按 [空格] 时,智能感知会自动选择任何内容第一个类是以“X”开头,通常是一些“XmlWhatever”类

但是,如果我将方法签名更改为:

void SomeMethod(Expression<Action<T>> a);

然后,如果我开始输入相同的用法,则按 [x][space] 会输入实际的字母“x” 。似乎智能感知处理放入 lambda 的参数是 Expression<> 类型,而不是 Action<> 类型。

有什么方法可以让 VS 智能感知正确处理第一种情况吗?我实际上无法传递表达式,因为我的预期用法是:

someClass.SomeMethod(x => x.Property1 = 123);

这会导致错误 CS0832: 表达式树可能不包含赋值运算符 因此,我确实希望传入 Action<> ,我实际上并不需要表达式,它只是修复智能感知。

这确实很烦人,因为当我输入操作参数时,我最终不得不按 [x][esc][space][=][>] 来获取文本“x =>”,添加“escape”按键以关闭智能感知弹出窗口。有什么想法或想法吗?


编辑:

好吧,我实际上必须重新表述一下我的问题。我上面的示例代码并不完全准确。 VS intellisense 似乎就是这种情况:

这是正确处理的:

public static T SomeMethod<T>(Action<T> actions) where T : class

但是 intellisense 没有正确处理:

public static T IsT4<T>(params Action<T>[] actions) where T : class

所以看来 intellisense 不喜欢 params

I'm working on a utility class and one of my methods is defined:

void SomeMethod(Action<T> a);

The awkward thing is that in Visual Studio 2010, when you start typing:

someClass.SomeMethod(x ...

when you press the [x] key, then [space], the intellisense automatically selects whatever the first class was that started with "X", typically some "XmlWhatever" class

However, if I change my method signature to:

void SomeMethod(Expression<Action<T>> a);

Then if I start typing the same usage, pressing [x][space] puts the actual letter 'x'. It seems the intellisense handles putting in a lambda were a parameter of type Expression<> is expected, but not of type Action<>.

Is there some way to make the VS intellisense handle the first case properly? I can't actually pass in an Expression because my intended usage is:

someClass.SomeMethod(x => x.Property1 = 123);

which leads to the error CS0832: An expression tree may not contain an assignment operator
Hence I do want the Action<> to be passed in, I don't really need an Expression, it just fixes the intellisense.

It is really annoying as it is, because as I type in the Action parameters, I end up having to press [x][esc][space][=][>] to get the text "x =>", adding the "escape" keypress to close the intellisense popup. Any thoughts or ideas?


Edit:

OK I actually have to re-phrase my question a bit. My sample code above wasn't exactly accurate. It seems that this is the case with VS intellisense:

This IS handled correctly:

public static T SomeMethod<T>(Action<T> actions) where T : class

but this is NOT handled correctly by intellisense:

public static T IsT4<T>(params Action<T>[] actions) where T : class

So it seems intellisense doesn't like the params.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-12-02 14:01:56

您可以使用显式数组创建语法来解决此问题:

obj.IsT4(new Action<SomeType>[] { x => ...

您还可以创建 IsT4 方法的额外重载,该方法仅需要 1 个操作委托。

You can use the explicit array creation syntax to work around this problem:

obj.IsT4(new Action<SomeType>[] { x => ...

You can also create an additional overload of the IsT4 method which takes exactly 1 action delegate.

小…楫夜泊 2024-12-02 14:01:56

工具|选项| C# |智能感知

取消选中“按空格键提交”。

在此处输入图像描述

Tools | Options | C# | Intellisense

Uncheck 'Committed by pressing the space bar'.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文