简体   繁体   中英

How to invoke a method in a C# generics class

I am new to C# generics. I have several classes with identical methods. I want to create a generic class which will be of type belonging to any of those classes and invoke the methods from them. I am not sure whether this is possible, or there is a different way of doing it. I have given my code sample below. I have included two representative classes named, EastCoastStores and WestCoastStores. When I try to invoke the method using reflection, I am getting null reference exception for GetMethod("GetZipCode") in the GetZipCode() method of the MyGenerics class. I have searched the internet, but could not an appropriate solution.

//Two classes with identical methods
public class EastCoastStores
{
    private string zip;

    public void SetZipCode(string zip)
    { this.zip = zip; }

    public string GetZipCode()
    { return zip; }
 }

public class WestCoastStores
{
    private string zip;

    public void SetZipCode(string zip)
    { this.zip = zip; }

    public string GetZipCode()
    { return zip; }
}

//Generic class which can accept either of the above classes
public class MyGenerics<T>
{
    private T selectedValues;
    public MyGenerics(T selectedValues)
    {
        this.selectedValues = selectedValues;
    }
    public String GetZipCode()
    {
        Type typeParameterType = typeof(T);
        var getZipCode = typeParameterType.GetMethod("GetZipCode");
        var val = 

typeParameterType.GetType().GetMethod("GetZipCode").Invoke(typeParameterType, 
 null);
        return val.ToString();
    }
}

//Accessing the class method
public static void Main(string[] args)
    {
        EastCoastStores eastCoastStores = new EastCoastStores();
        eastCoastStores.SetZipCode("12345");
        MyGenerics<EastCoastStores> orderAction = new 
  MyGenerics<EastCoastStores>(eastCoastStores);
        var val = orderAction.GetZipCode();

    }

I don't understand why you're using generics and reflection. What you're trying to do could be easily achieved using inheritance and polymorphism, without involving reflection that isn't something that should be used carelessly as performance impact could be relevant.

You can follow what I said in the comment, but it'd be too much for an implementation similar to what you posted (several classes with identical methods).

You can use an interface (eg ICoastStore ) that is implemented by EastCoastStore and WestCoastStore types. Then you don't need to use reflection and the implementation of MyGenerics will be much simpler.

public class EastCoastStores : ICoastStore
{
     private string zip;

     public void SetZipCode(string zip)
     { this.zip = zip; }

     public string GetZipCode()
     { return zip; }
}

public class WestCoastStores : ICoastStore
{
     private string zip;

     public void SetZipCode(string zip)
     { this.zip = zip; }

     public string GetZipCode()
     { return zip; }
}

public interface ICoastStore
{
     void SetZipCode(string zip);

     string GetZipCode();
}

public class MyGenerics
{
     private ICoastStore selectedValues;

     public MyGenerics(ICoastStore selectedValues)
     {
          this.selectedValues = selectedValues;
     }

     public string GetZipCode()
     {
          return selectedValues.GetZipCode();
     }
 }

static void Main(string[] args)
{
      EastCoastStores eastCoastStores = new EastCoastStores();
      eastCoastStores.SetZipCode("12345");
      MyGenerics orderAction = new MyGenerics(eastCoastStores);
      var val = orderAction.GetZipCode();
}

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