如何在C#泛型类中调用方法

如何在C#泛型类中调用方法,c#,generics,reflection,C#,Generics,Reflection,我不熟悉C#泛型。我有几个方法相同的类。我想创建一个泛型类,它属于这些类中的任何一个,并从中调用方法。我不确定这是否可行,或者有不同的方法。我在下面给出了我的代码示例。我包括了两个有代表性的课程,分别是EastCoastStores和WestCoastStores。当我尝试使用反射调用该方法时,我在MyGenerics类的GetZipCode()方法中得到GetMethod(“GetZipCode”)的null引用异常。我在网上搜索过,但找不到合适的解决方案 //Two classes with

我不熟悉C#泛型。我有几个方法相同的类。我想创建一个泛型类,它属于这些类中的任何一个,并从中调用方法。我不确定这是否可行,或者有不同的方法。我在下面给出了我的代码示例。我包括了两个有代表性的课程,分别是EastCoastStores和WestCoastStores。当我尝试使用反射调用该方法时,我在MyGenerics类的GetZipCode()方法中得到GetMethod(“GetZipCode”)的null引用异常。我在网上搜索过,但找不到合适的解决方案

//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();

    }
//两个方法相同的类
公共类东海岸商店
{
私人字符串拉链;
公共void SetZipCode(字符串zip)
{this.zip=zip;}
公共字符串GetZipCode()
{返回zip;}
}
公共级西海岸商店
{
私人字符串拉链;
公共void SetZipCode(字符串zip)
{this.zip=zip;}
公共字符串GetZipCode()
{返回zip;}
}
//可以接受上述任一类的泛型类
公共类泛型
{
私有T选择值;
公共MyGenerics(T selectedValues)
{
this.selectedValues=selectedValues;
}
公共字符串GetZipCode()
{
类型类型参数类型=类型(T);
var getZipCode=typeParameterType.GetMethod(“getZipCode”);
var val=
typeParameterType.GetType().GetMethod(“GetZipCode”).Invoke(typeParameterType,
无效);
返回值ToString();
}
}
//访问类方法
公共静态void Main(字符串[]args)
{
EastCoastStores EastCoastStores=新的EastCoastStores();
eastCoastStores.SetZipCode(“12345”);
MyGenerics orderAction=新建
MyGenerics(东海岸商店);
var val=orderAction.GetZipCode();
}

我不明白为什么要使用泛型和反射。 使用继承和多态性可以很容易地实现您要做的事情,而不需要涉及反射,因为反射不应该被随意使用,因为性能影响可能是相关的

您可以按照我在评论中所说的去做,但是对于一个类似于您所发布的实现(几个具有相同方法的类)来说,这太过分了

您可以使用由
EastCoastStore
WestCoastStore
类型实现的接口(例如
ICoastStore
)。这样,您就不需要使用反射,
MyGenerics
的实现将简单得多

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();
}

为什么要使用反射?如果T是从WestCoastShore和EastCoashore类型继承而来的类型,那就简单多了……为什么要使用泛型呢?如果您有两个使用“相同”方法的类,那么公共接口似乎更合适。可能是Hi Alberto的副本,通常我会按照您所描述的那样做。不幸的是,这是一个现有的应用程序,我不被允许实现继承,因为它会涉及很多更改。是的,我完全理解。过去我自己也处理过遗留代码,根据经验,我发现唯一的方法是重构整个项目(确实有很多更改),而不是实现一件小事来修复一个bug或在现有的意大利面代码库上添加一个特性,因为这实际上会大大降低代码的可维护性。正如我所说,应该谨慎使用反射。如果继续,现有应用程序将遭遇性能瓶颈。考虑是否需要在将来应用更多的更改。