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 Actio开发者_如何学Pythonn<>
.
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
.
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.
Tools | Options | C# | Intellisense
Uncheck 'Committed by pressing the space bar'.
精彩评论