接受 2 个 lambda 的方法存在问题

发布于 2024-09-07 11:33:38 字数 1339 浏览 2 评论 0原文

我有以下类:

    public class MyClass<T> where T : class
    {
         public void Method1<TResult>(T obj, Expression<Func<T, TResult>> expression)
         {
             //Do some work here...
         }

         public void Method2<TResult>(T obj, Expression<Func<T, TResult>> expression1, Expression<Func<T, TResult>> expression2)
         {
             //Do some work here...
         }
    }

我可以像这样调用 Method1:

MyClass<SomeOtherClass> myObject = new MyClass<SomeOtherClass>();

myObject.Method1(someObject, x => x.SomeProperty);

但是当我尝试调用 Method2:

MyClass<SomeOtherClass> myObject = new MyClass<SomeOtherClass>();

myObject.Method2(someObject, x => x.SomeProperty, x => x.SomeOtherProperty);

我收到以下编译时错误:

Error 1 The type arguments for method 'MyClass.Method2<SomeOtherClass>.Method2<TResult>(SomeOtherClass obj, System.Linq.Expressions.Expression<System.Func<SomeOtherClass,TResult>>, System.Linq.Expressions.Expression<System.Func<SomeOtherClass,TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 

如何创建一个接受两个 lambda 并按我的预期调用它的方法?

I have the following class:

    public class MyClass<T> where T : class
    {
         public void Method1<TResult>(T obj, Expression<Func<T, TResult>> expression)
         {
             //Do some work here...
         }

         public void Method2<TResult>(T obj, Expression<Func<T, TResult>> expression1, Expression<Func<T, TResult>> expression2)
         {
             //Do some work here...
         }
    }

I can call Method1 like this:

MyClass<SomeOtherClass> myObject = new MyClass<SomeOtherClass>();

myObject.Method1(someObject, x => x.SomeProperty);

But when I try to call Method2:

MyClass<SomeOtherClass> myObject = new MyClass<SomeOtherClass>();

myObject.Method2(someObject, x => x.SomeProperty, x => x.SomeOtherProperty);

I get the following compile-time error:

Error 1 The type arguments for method 'MyClass.Method2<SomeOtherClass>.Method2<TResult>(SomeOtherClass obj, System.Linq.Expressions.Expression<System.Func<SomeOtherClass,TResult>>, System.Linq.Expressions.Expression<System.Func<SomeOtherClass,TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 

How can I make an method that accept two lambdas and call it like I intended?

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

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

发布评论

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

评论(3

眼眸里的快感 2024-09-14 11:33:38

SomePropertySomeOtherProperty 是否具有相同的类型?如果没有,那就是你的问题,因为你使用的是一个 TResult 类型参数。

解决方案只是使用两个类型参数:

public void Method2<TResult1, TResult2>(T obj, Expression<Func<T, TResult1>> expression1, Expression<Func<T, TResult2>> expression2)
{
    //Do some work here...
}

Do SomeProperty and SomeOtherProperty have the same type? If not, there's your problem, since you're using one TResult type parameter.

The solution is just to use two type parameters:

public void Method2<TResult1, TResult2>(T obj, Expression<Func<T, TResult1>> expression1, Expression<Func<T, TResult2>> expression2)
{
    //Do some work here...
}
灼疼热情 2024-09-14 11:33:38

您是否尝试过使用 2 个类型参数?

例如:

void Method2<TResult1, TResult2>(T obj, 
   Expression<Func<T, TResult1>> expression1, 
   Expression<Func<T, TResult2>> expression2)

Have you tried using 2 type parameters instead?

Eg:

void Method2<TResult1, TResult2>(T obj, 
   Expression<Func<T, TResult1>> expression1, 
   Expression<Func<T, TResult2>> expression2)
余生一个溪 2024-09-14 11:33:38

您可以尝试明确指定类型参数。

myObject.Method2<string>(
  someObject,
  x => x.SomeProperty,
  x => x.SomeOtherProperty);

如果这不起作用(假设 SomeProperty 和 SomeOtherProperty 是不同的类型),您可以在方法声明中允许附加类型信息。

public void Method2<TResult1, TResult2>
(
  T obj,
  Expression<Func<T, TResult1>> expression1,
  Expression<Func<T, TResult2>> expression2
) 

You could try specifying the type argument explicity.

myObject.Method2<string>(
  someObject,
  x => x.SomeProperty,
  x => x.SomeOtherProperty);

If that doesn't work (say SomeProperty and SomeOtherProperty are different types) you could allow for additional type information in the method declaration.

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