[英]c# pass class as a parameter
I want to pass a class as parameter.我想传递一个类作为参数。
So what I want to do is the pass a class, for example: "Customer" to a method.所以我想要做的是传递一个类,例如:“客户”到一个方法。 I want to do this because then I can also pass, for example: "Contract" as class to the same method.
我想这样做是因为我也可以将例如:“Contract”作为类传递给相同的方法。 This way I don't need to make a method for every class.
这样我就不需要为每个类都创建一个方法。
Side info: I am using EntityFramework 6, MVC 5, Nest 1.0 and ElasticSearch 1.4补充信息:我使用的是 EntityFramework 6、MVC 5、Nest 1.0 和 ElasticSearch 1.4
The concept is that we place stuff in ElasticSearch and that we then can do a search.这个概念是我们将东西放在 ElasticSearch 中,然后我们可以进行搜索。 The search query is:
搜索查询是:
SearchElasticClient.Search<Customer>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXXX"))))));
And for contract:对于合同:
SearchElasticClient.Search<Contract>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXXX"))))));
As you can see, if I want to do a search for every type we have, then I need to copy paste this query like 20 times at least.如您所见,如果我想对我们拥有的每种类型进行搜索,那么我需要至少复制粘贴此查询 20 次。
I don't like copy pasting because the code is not proper and when I need to change it, it will be a lot of work.我不喜欢复制粘贴,因为代码不正确,当我需要更改它时,这将是很多工作。
So I want to create a method that takes my class as argument or something like that so that I can make a generic method that reuses this block of code.所以我想创建一个方法,将我的类作为参数或类似的东西,以便我可以创建一个重用这个代码块的通用方法。
So for our example:所以对于我们的例子:
My (Enitity Framework) classes:我的(实体框架)类:
public class Customer{
public int CustomerID {get;set;}
public String CustomerName {get;set;}
}
public class Contract{
public int ContractID {get;set;}
public String ContractName {get;set;}
}
relation(s) between the classes is for me irrelavent so I left them out.课程之间的关系对我来说无关紧要,所以我把它们排除在外。
Then in my HomeController I would like something like然后在我的 HomeController 中,我想要类似的东西
public class HomeController : Controller
{
...
public ActionResult Search(String textToSearch)
{
//So here you see that I want to use the same method for both classes.
Customer customer = Helpers.SearchHelper.Search(textToSearch);
Contract contract = Helpers.SearchHelper.Search(textToSearch);
}
}
Then my SearchHelper would be something like:然后我的 SearchHelper 将是这样的:
public static class SearchHelper
{
public static ElasticClient SearchElasticClient
{
get
{
Uri uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri, "default_INDEX");
return new ElasticClient(setting);
}
}
public static void SearchTest(String textToSearch, MyClass)
{
var test = SearchElasticClient
.Search<Customer>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXXX"))))));
}
}
As you can see, now I set my class "Customer" fixed in my code.如您所见,现在我在我的代码中设置了我的类“Customer”。 I want to replace that with a variable or something.
我想用变量或其他东西替换它。
Now what I have tried :现在我已经尝试过:
public static void SearchTest<T>(String textToSearch)
{
var test = SearchElasticClient
.Search<T>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXXX"))))));
}
Here I get the compile error: "Cannot convert lambda expression to type 'Nest.ISearchRequest' because it is not a delegate type."在这里,我收到编译错误:“无法将 lambda 表达式转换为类型 'Nest.ISearchRequest',因为它不是委托类型。”
I am not familiar with delegation and how it works and if I can use it, so if delegation is something I need, please provide me enough details.我不熟悉委托及其工作原理以及我是否可以使用它,因此如果我需要委托,请提供足够的详细信息。
I also tried:我也试过:
public static void SearchTest(String textToSearch, Type myClass)
{
var test = SearchElasticClient
.Search<myClass>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXXX"))))));
}
Then it gives me the compile error: "The Type or namespace 'myClass' could not be found."然后它给了我编译错误:“找不到类型或命名空间‘myClass’。” I understand why I get this error, so I know that it will be more something like public static void Search(..){..} but I have no idea how to implement it.
我明白为什么我会收到这个错误,所以我知道它更像是 public static void Search(..){..} 但我不知道如何实现它。
I hope this is a better explanation about my problem.我希望这是对我的问题的更好解释。
So it is an implemantion of the "Nest" search and I want to avoid copy pasting the search query.所以它是“嵌套”搜索的实现,我想避免复制粘贴搜索查询。
Thanks in advance提前致谢
I believe what you want to do is make Search
generic我相信你想做的是让
Search
变得通用
public static classToPass Search<classToPass>()
Then use it like this然后像这样使用它
Test x = Helper.Search<Test>(); //Test = class as definied above
TestTwo y = Helper.Search<TestTwo>();
Make the Search
method generic.使
Search
方法通用。 A generic argument is, more or less, a parameter that is a type, rather than an object.泛型参数或多或少是一个类型的参数,而不是一个对象。
public static class Helper
{
public static object Search<T>(T classToPass)
{
SearchElasticClient
.Search<T>(body => body
.AllIndices()
.Size(500)
.Query(query => query
.Bool(@bool => @bool
.Must(must => must
.QueryString(qs => qs
.Query("XXX"))))));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.