C#如果泛型方法中的类型参数是嵌套类,则将其分解为类型参数

C#如果泛型方法中的类型参数是嵌套类,则将其分解为类型参数,c#,generics,nested,C#,Generics,Nested,例如,我试图将一个嵌套类传递给一个泛型方法来计算它的所有类 SharedClass.FindParentClass<GrandParent.Parent.Child>(); SharedClass.FindParentClass(); 一般方法: public void FindParentClass<T>() where T: ISomeInterface, new() { //Break down T to all of its classes } publ

例如,我试图将一个嵌套类传递给一个泛型方法来计算它的所有类

SharedClass.FindParentClass<GrandParent.Parent.Child>();
SharedClass.FindParentClass();
一般方法:

public void FindParentClass<T>() where T: ISomeInterface, new()
{
  //Break down T to all of its classes
}
public void FindParentClass(),其中T:ISomeInterface,new()
{
//将T分解为它的所有类
}
我想避免这样做:

SharedClass.FindParentClass<GrandParent,GrandParent.Parent,GrandParent.Parent.Child>();
SharedClass.FindParentClass();
适用于上述代码的通用方法:

public void FindParent<TGrandParent, TParent, TChild>() where TGrandParent : IGrandParent, new()
                                                         where TParent : IParent, new()
                                                          where TChild : IChild, new()
{
 //all I have to do now is place the type parameters there where I want them    
}
public void FindParent(),其中TGrandParent:IGrandParent,new()
其中t租金:i租金,新()
其中TChild:IChild,new()
{
//我现在要做的就是把类型参数放在我想要的地方
}

我不允许更改用作类型参数的类,因此每个类都继承一个不同的接口,并具有一个无公共参数的构造函数。

为了按照我的预期方式工作,我必须编写另一个(私有)泛型方法来接受所有三个类型参数:

private void PerformWith<GrandParent, Grandparent.Parent, GrandParent.Parent.Child>()
{
         //Perform something
}
private void PerformWith()
{
//表演
}
因此,在我的第一个通用方法中,我将使用反射:

  • 使用GetTypeInfo()确定所有父类型。ReflectedType
  • 要获取新方法,请将其设置为泛型并调用它
  • 看起来是这样的:

    public void FindParentClass<T> where T: ISomeInterface, new()
    {
       var parentClass = typeof(T).GetTypeInfo().ReflectedType;
       var grandparentClass = parentClass.GetTypeInfo().ReflectedType;
    
       var method = MethodBase.GetCurrentMethod().DeclaringType.GetMethod("PerformWith", BindingFlags.NonPublic());
       var genericMethod = method.MakeGenericMethod(new Type[] { grandparentClass, parentClass, typeof(T) });
       genericMethod.Invoke(null, null);
    }
    
    public void FindParentClass其中T:isome接口,new()
    {
    var parentClass=typeof(T).GetTypeInfo().ReflectedType;
    var-grandrentclass=parentClass.GetTypeInfo().ReflectedType;
    var method=MethodBase.GetCurrentMethod().DeclaringType.GetMethod(“PerformWith”,BindingFlags.NonPublic());
    var genericMethod=method.MakeGenericMethod(新类型[]{grandrentclass,parentClass,typeof(T)});
    调用(null,null);
    }
    
    为了按照我预期的方式工作,我必须编写另一个(私有)通用方法,该方法接受所有三个类型参数:

    private void PerformWith<GrandParent, Grandparent.Parent, GrandParent.Parent.Child>()
    {
             //Perform something
    }
    
    private void PerformWith()
    {
    //表演
    }
    
    因此,在我的第一个通用方法中,我将使用反射:

  • 使用GetTypeInfo()确定所有父类型。ReflectedType
  • 要获取新方法,请将其设置为泛型并调用它
  • 看起来是这样的:

    public void FindParentClass<T> where T: ISomeInterface, new()
    {
       var parentClass = typeof(T).GetTypeInfo().ReflectedType;
       var grandparentClass = parentClass.GetTypeInfo().ReflectedType;
    
       var method = MethodBase.GetCurrentMethod().DeclaringType.GetMethod("PerformWith", BindingFlags.NonPublic());
       var genericMethod = method.MakeGenericMethod(new Type[] { grandparentClass, parentClass, typeof(T) });
       genericMethod.Invoke(null, null);
    }
    
    public void FindParentClass其中T:isome接口,new()
    {
    var parentClass=typeof(T).GetTypeInfo().ReflectedType;
    var-grandrentclass=parentClass.GetTypeInfo().ReflectedType;
    var method=MethodBase.GetCurrentMethod().DeclaringType.GetMethod(“PerformWith”,BindingFlags.NonPublic());
    var genericMethod=method.MakeGenericMethod(新类型[]{grandrentclass,parentClass,typeof(T)});
    调用(null,null);
    }
    
    这是不可能的。看一看。您可以使用反射来确定声明对象的类型:。泛型只是一种类型的替代品,而不是文件中的类列表。没有获取父类中所有嵌套类的通用约束。然而,这似乎很奇怪。为什么不简单地让所有嵌套类实现一个commin接口并将其用作约束呢?这是不可能的。看一看。您可以使用反射来确定声明对象的类型:。泛型只是一种类型的替代品,而不是文件中的类列表。没有获取父类中所有嵌套类的通用约束。然而,这似乎很奇怪。为什么不让您的所有嵌套类实现一个commin接口并将其用作约束呢?