问题
Basically, I've created a helper method for a Validation object I'm using. The code is like so:
public class ValidationSet<TSource> : ValidationSet
{
public void AddValidationErrorFor<TProperty>(Expression<Func<TSource, TProperty>> propertyLambda, string errorMessage, string data = null)
{
// extension method
PropertyInfo property = propertyLambda.GetPropertyInfo();
Add(new ValidationItem
{
Key = property.Name,
Message = errorMessage,
DataMessage = data
});
}
}
public class ValidationSet : List<ValidationItem>
{
public void AddValidationError(string key, string errorMessage)
{
Add(new ValidationItem
{
Key = key,
Message = errorMessage
});
}
}
Effectively, what happens (and this works), is that I can write something like this:
public class SomeObject
{
public int Id { get; set; }
public string SomeValue { get; set; }
}
And then when I want to validate it, I can write something like this:
ValidationSet<SomeObject> validationSet = new ValidationSet<SomeObject>();
if(SomeValue.Contains("SomeOtherValue"))
validationSet.AddValidatorErrorFor(x => x.SomeValue, "Some Error");
This all currently compiles. However, I don't get intellisense for my lambda expression in the last line. I have to manually type x.SomeValue
, even though it all compiles fine. Does anyone know what is missing to get the intellisense?
回答1:
I reproduced your setup like so...
public class ValidateThing<TSource>
{
public void AddValidation<TProperty>(Expression<Func<TSource, TProperty>> expr)
{
//...
}
}
public static class Tester
{
public static void Test()
{
ValidateThing<string> v = new ValidateThing<string>();
v.AddValidation(s => s.Length);
}
}
As soon as I typed the s.
after s =>
, I got intellisense for the members of String
. Does this code snippet produce the same behavior as your code?
EDIT: Oh, I suppose it's relevant to ask what version of Visual Studio you're using.
回答2:
IntelliSense in the IDE doesn't have 1-1 parity with code that compiles. It has to bind expressions based on partially typed structures and hence incomplete information. This is not always possible to do.
One area which is particularly difficult is IDE inference of lambda expressions (particularly expression trees). This is likely just a case the IDE doesn't handle well for intellisense
回答3:
@Tejs You got me on the right track. Although I didn't have a nullable parameter, I did have some extra parameters. Two of them to be exact. I removed them and then IntelliSense worked.
So I put them back and created an overloaded version of the extension without the extra parameters and it still worked.
As another test, I commented the extension that had only the one parameter, and verified IntelliSense still worked. I then added a second parameter to the extension that only had the one parameter, and IntelliSense stopped working.
So in my case at least, I needed a version that only had 1 parameter.
Hope this helps someone else.
Oh and I'm using VS 2015.
来源:https://stackoverflow.com/questions/5475406/not-getting-intellisense-for-lambda-expression