简体   繁体   中英

How to write generic way method in C#

I have individual methods such as:

 public string Method1()
   {
      class1 c1= new class1 ();
      var data = c1.GetMainData(Id);
      var value= c1.GetValue(data,c1);
   }

  public string Method2()
    {
       class2 c2= new class2 ();
       var data = c2.GetMainData(Id);
       var value= c2.GetValue(data,c2);
    }

  public string Method3()
    {
       class3 c3 = new class3 ();
       var data = c3.GetMainData(Id);
       var value= c3.GetValue(data,c3);
    }
                   

From the above functions, classes class1 , class2 , and class3 are different but the method names GetMainData and GetValue are same in the class.

  • method names are same and passing class object to method and have different functionality and returning string.

Please help me to write a generic single method to handle?

have you tried writing a generic method?

interface IMyInterface
{
    string GetMainData(string id);
    string GetValue(string data);
}

string getValue<T>() where T:IMyInterface,new()
{
    var c = new T();
    var data = c.GetMainData(Id);
    return c.GetValue(data);
}

the where clause indicates the minimum requirements of T , in this case any class passed as T must inherit from IMyInterface and have a empty constructor, because of this the compiler knows that T must have a new() and all methods and properties specified on the IMyInterface interface

additional note : if you are using an interface then the generics are probably not even required as you can call the instance functions with out knowing exactly what class they are

string getValue(IMyInterface c)
{
    var data = c.GetMainData(Id);
    return c.GetValue(data);
}

the generics is generally only required when you need the return or inputs to be the same type as T

the most common generic i write is a collection extentions

public static Add(this ICollection<T> col, IEnumerable<T> items)
{
   foreach(var item in items)
       col.Add(item)
}

here if the items are not the same type as the collection then the code doesn't work, however we actually don't care what type the items and collection are as long as they match

Unlike C++, C# has only limited duck typing, and only for compiler internals (disposing) or extension methods. Neither of them apply here.

You have three options here. You either use a common polymorphic base type that allows you to call your methods:

interface I { 
    object GetMainData(int id);
    object GetValue(int data);
}

class class1 : I {
    // implement the interface
}

// same for class2 and class3

public string Method<T>() where T: I, new()
{
     var c = new T();
     var data = c.GetMainData(Id);
     var value= c.GetValue(data);
     // return something
}

You can also use reflection to call methods by name from unrelated objects.

And of course, you can use dynamic to create a compiler site to do the same for you.

Note that the last two are orders of magnitude slower than the first option, though dynamic 's cost is mostly at the first call to build the site and compile the new generated code, subsequent calls are pretty fast.

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