没有获得 Lambda 表达式的 Intellisense
基本上,我为我正在使用的验证对象创建了一个辅助方法。代码是这样的:
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
});
}
}
实际上,会发生什么(并且这有效),我可以写这样的东西:
public class SomeObject
{
public int Id { get; set; }
public string SomeValue { get; set; }
}
然后当我想验证它时,我可以写这样的东西:
ValidationSet<SomeObject> validationSet = new ValidationSet<SomeObject>();
if(SomeValue.Contains("SomeOtherValue"))
validationSet.AddValidatorErrorFor(x => x.SomeValue, "Some Error");
这一切当前都已编译。但是,我在最后一行中没有获得 lambda 表达式的智能感知。我必须手动输入 x.SomeValue,尽管它都编译得很好。有谁知道获得智能感知缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我像这样重现了您的设置...
一旦我键入
s。
之后s =&gt;
,我为String .此代码段是否产生与代码相同的行为?
编辑:哦,我想询问您正在使用哪种版本的Visual Studio是相关的。
I reproduced your setup like so...
As soon as I typed the
s.
afters =>
, I got intellisense for the members ofString
. 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.
IDE 中的 IntelliSense 与可编译的代码没有 1-1 奇偶校验。它必须根据部分类型的结构绑定表达式,因此信息不完整。这并不总是可行的。
特别困难的一个领域是 lambda 表达式(特别是表达式树)的 IDE 推断。这可能只是 IDE 不能很好地处理智能感知的情况
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
@Tejs 你让我走上了正轨。虽然我没有可为空的参数,但我确实有一些额外的参数。准确地说是其中两个。我删除了它们,然后 IntelliSense 就开始工作了。
所以我把它们放回去并创建了一个扩展的重载版本,没有额外的参数,它仍然有效。
作为另一项测试,我评论了只有一个参数的扩展,并验证了 IntelliSense 仍然有效。然后,我向只有一个参数的扩展添加了第二个参数,IntelliSense 停止工作。
所以至少就我而言,我需要一个只有 1 个参数的版本。
希望这对其他人有帮助。
哦,我正在使用 VS 2015。
@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.