简体   繁体   中英

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. 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

The concept is that we place stuff in ElasticSearch and that we then can do a search. 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.

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

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:

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. 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."

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." 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.

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

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. 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"))))));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM