简体   繁体   English

在C#中,如何将类型化数组传递给泛型函数?

[英]In C#, how can I pass typed array into generic function?

I have a collection: 我有一个收藏:

 IQueryable<Car>

and i want to pass into a generic function that takes in as a parameter: 我想传递给一个以参数为参数的通用函数:

 IQueryable<T>

something like (which is sitting in a base class) 像(坐在基类中)

   public abstract class  BaseController<T> : ControllerBase  where T : BaseObj, new()
  {

      public IQueryable<T> FilterEntities(IQueryable<T> entities)
     {
     }
  }

what is the right way to pass this in? 正确的方法是什么? Cast?, safety cast? 演员?安全演员?

public IQueryable<TQueryable> FilterEntities<TQueryable>(
    IQueryable<TQueryable> entities)
{
}

You need to make the method a generic method (the <T> after the function name before the parens tells the compiler the method is generic). 您需要使该方法成为通用方法(函数名称后面的<T>在Parens告诉编译器该方法是通用方法之前)。

The type parameter TQueryable of the method differs from the class type. 方法的类型参数TQueryable与类类型不同。 If you don't want it to your method signature should be fine. 如果您不想要它,则方法签名应该没问题。

your problem is that your method isn't generic, the class is. 您的问题是您的方法不是通用的,类是。 Notice there' no <T> after then method name, than mean you have to pass in an IQueryable of what ever was yoused for the type argument for the class. 注意,在方法名之后没有<T> ,这意味着您必须传递一个IQueryable,该IQueryable是您为类的类型参数使用的内容。

Eg if the object was instantiated like this: 例如,如果对象是这样实例化的:

new BaseController<BaseObject>(..) //ignoring that BaseController is abstract

you'd need to pass an IQueryable<BaseObject> so in your case where you wish to pass in an IQueryable<Car> the type of controller needs to derive from BaseController<Car> 您需要传递一个IQueryable<BaseObject>因此在您希望传递IQueryable<Car>的情况下,需要从BaseController<Car>派生控制器的类型

If on the other hand you wish the method to be generic change the signature of the method to 另一方面,如果您希望该方法是通用的,请将方法的签名更改为

public IQueryable<TElement> FilterEntities<TElement>
            (IQueryable<TElement> entities)

that does not include the type contraint on T you have on the type parameter for the class. 不包含您在类的类型参数上具有的T上的类型约束。 If this is should be enforced for the generic method the signature needs to be: 如果应为通用方法强制执行此操作,则签名必须为:

public IQueryable<TElement> FilterEntities<TElement>(IQueryable<TElement> entities) 
            where TElement : BaseObj, new()

EDIT If you wish to have a "default" method that simply uses T as the type argument you will have to implement it as a non generic method just as you have in your code so the class would be: 编辑如果您希望有一个仅使用T作为类型参数的“默认”方法,则必须像代码中那样将其实现为非通用方法,因此该类将是:

  public abstract class  BaseController<T> : ControllerBase  
                where T : BaseObj, new()
  {

     public IQueryable<T> FilterEntities(IQueryable<T> entities)
     {
            return FilterEntities<T>(entities);
     }

     public IQueryable<TElement> FilterEntities<TElement>(IQueryable<TElement> entities) 
                where TElement : BaseObj, new()
  }

Define the FilterEntities as below 如下定义FilterEntities

public IQueryable<T> FilterEntities<T>(IQueryable<T> entities)
{
}

Now you can pass IQueryable as below 现在您可以通过IQueryable如下

    IQueryable<Car> x = null; //Initialize it
    FilterEntities<Car>(x);

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

相关问题 C# - 在通用 Class 中,如何设置通用类型的 Function 委托? - C# - In a Generic Class, How Can I Set a Generically-Typed Function Delegate? 如何在C#中传递通用数组? - How to pass a generic array in C#? C#试图简化通用类型接口的通用扩展功能 - C# trying to simplify Generic Extension Function for Generic Typed Interface 使用C#,如何将二进制数据的字节数组转换为对数据建模的自定义类型对象? - With C#, how can I convert a byte array of binary data into a custom-typed object that models the data? 如何将JSON数组传递给C#WebBrowser控件中的javascript函数? - How can I pass a JSON array to a javascript function in the C# WebBrowser Control? 如何在C#中获取动态类型数组的成员? - How do I get members of a dynamically typed array in C#? 如何将整数数组传递给方法:反射C# - How can I pass an integer array to a method: Reflection C# 如何在 C# 中访问匿名类型 JArray 的字段? - How can i access fields of anonymous typed JArray in C#? 如何在C#中创建一个类型化的IEnumerable? - How can I create a typed IEnumerable in C#? C#:如何将类传递给单独dll中的函数 - C#: How can I pass a class to a function in separate dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM