无法获得Lambda表达式的Intellisense [英] Not getting Intellisense for Lambda Expression
问题描述
基本上,我已经为正在使用的Validation对象创建了一个辅助方法.代码是这样的:
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");
当前全部编译.但是,在最后一行中,我对lambda表达式没有任何理解.我必须手动键入x.SomeValue
,即使它们都可以正常编译.有谁知道获得智能感知所需的东西吗?
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?
推荐答案
我像这样复制了您的设置...
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);
}
}
在s =>
之后键入s.
时,我就对String
的成员有了智能感知.此代码段产生的行为与您的代码相同吗?
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?
编辑:哦,我想问您使用的是哪个版本的Visual Studio是很重要的.
EDIT: Oh, I suppose it's relevant to ask what version of Visual Studio you're using.
这篇关于无法获得Lambda表达式的Intellisense的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!