简体   繁体   English

我如何使用C#将类的多属性作为方法的参数传递

[英]How i can pass multi property of class as parameter of method using C#

I have a class that will be inherit in onother class. 我有一个将在onother类中继承的类。 The content of class: 课程内容:

public T First(Expression<Func<T, string>> OrderBy = null)
{
   string orderby=string.Empty;
   if (OrderBy != null)
   {
      System.Reflection.PropertyInfo orderbyinfo= (System.Reflection.PropertyInfo)  ((MemberExpression)OrderBy.Body).Member;
            orderby = orderbyinfo.Name;
   }
   ...

} }

But error "Can not convert lambda expression to delegate type ..." with method: 但是出现错误“无法将lambda表达式转换为委托类型...”的方法:

public T First(Expression<Func<T, string>> OrderBy = null,Expression<Func<T, string>> GroupBy = null)
{
   string orderby=string.Empty;
   string groupby=string.empty
   if (OrderBy != null)
   {
      System.Reflection.PropertyInfo orderbyinfo= (System.Reflection.PropertyInfo)   ((MemberExpression)OrderBy.Body).Member;
            orderby = orderbyinfo.Name;
   }

   if (GroupBy != null)
   {
      System.Reflection.PropertyInfo groupbyinfo= (System.Reflection.PropertyInfo)  ((MemberExpression)GroupBy.Body).Member;
            groupby = groupbyinfo.Name;
   }
   ...

} }

And method as: 和方法为:

Article article = new Article();
article.Title="test";
article=article.First(x=>x.Title,y=>y.Status);

Please help me! 请帮我! thanks!!! 谢谢!!!

You get this error because Status probably isn't a string and your Func s can return only a string : 您会收到此错误,因为Status可能不是string并且Func只能返回一个string

public T First(
    Expression<Func<T, string>> orderBy = null,
    Expression<Func<T, string>> groupBy = null)

if you make them generic you'll be able to return anything. 如果将它们设为通用,则可以返回任何内容。 Because both properties can be of different types you'll need two generic types ( TProperty1 TProperty2 ), one for each of them: 因为这两个属性可以属于不同的类型,所以您将需要两种通用类型( TProperty1 TProperty2 ),每种类型都需要一个:

public T First<TProperty1, TProperty2>(
    Expression<Func<T, TProperty1>> orderBy = null, 
    Expression<Func<T, TProperty2>> groupBy = null)

Example: 例:

class C<T>
{        

    public T First<TProperty1, TProperty2>(
        Expression<Func<T, TProperty1>> orderBy = null, 
        Expression<Func<T, TProperty2>> groupBy = null)
    {
        string orderByName = string.Empty;
        string groupByName = string.Empty;
        if (orderBy != null)
        {
            System.Reflection.PropertyInfo orderByInfo = (System.Reflection.PropertyInfo)((MemberExpression)orderBy.Body).Member;
            orderByName = orderByInfo.Name;
        }

        if (groupBy != null)
        {
            System.Reflection.PropertyInfo groupByInfo = (System.Reflection.PropertyInfo)((MemberExpression)groupBy.Body).Member;
            groupByName = groupByInfo.Name;
        }
        ....
    }
}

Usage: here both properties are int s 用法:此处两个属性均为int

new C<string>().First(x => x.Length, x => x.Length);

However if they must be string s then you need to convert/cast the values to string s: 但是,如果它们必须是string s,那么您需要将值转换/转换为string s:

article = article.First(x=>x.Title, y=>y.Status.ToString());

Would you have read all error messages and posted all of them here you would have noticed that there is a line that tells you about the conversion error: 您是否已阅读所有错误消息并将其全部发布到此处,您会发现有一行告诉您有关转换错误的信息:

Error 1 Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type 错误1无法将lambda表达式转换为委托类型'System.Func',因为该块中的某些返回类型不能隐式转换为委托返回类型

Error 2 Cannot implicitly convert type 'int' to 'string' 错误2无法将类型'int'隐式转换为'string'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将类的属性作为方法的参数传递? - How can I pass a property of a class as a parameter of a method? 使用C#,如何使用参数访问VB类的默认属性的getter / setter? - Using C#, how can I access the getter/setter of a VB class's default property with parameter? 如何将方法结果作为参数传递给C#中的基类构造函数? - How to pass method result as parameter to base class constructor in C#? 我可以将条件作为参数传递给C#中的参数 - Can I pass a condition to a method as parameter in C# C#方法参数是该类的属性 - C# method parameter being a property of the class 我们可以将参数传递给C#属性吗? - Can we pass parameter to C# property? 给定一个包含类名称的字符串,然后我如何使用该类作为C#中的类型参数来调用泛型方法? - Given a string which holds the name of a class, how can I then call a generic method using that class as the type parameter in C#? 将 C# 类属性作为方法中的参数传递 - Pass a C# class property as argument in a method C#如何将类作为参数传递给方法并确定参数类的基类 - C# how to pass a class as a parameter into a method and determining the base class of the parameter class 如何正确地将输入参数传递给C#中的事件处理程序方法? - How can I correctly pass an input parameter to an event handler method in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM