简体   繁体   English

如何检查泛型类使用的类型

[英]how to check the Type used by a generic class

Consider the case where you have various classes derived from MyFaultBase . 考虑一下您具有从MyFaultBase派生的各种类的情况。 So when your web service needs to indicate fault it throws an exception of type FaultException<MySpecificFault> . 因此,当您的Web服务需要指示故障时,它将引发FaultException<MySpecificFault>类型的异常。

After catching this exception, how can you determine whether the FaultException<T> is bound to a class derived from MyFaultBase ? 捕获此异常之后,如何确定FaultException<T>是否绑定到从MyFaultBase派生的类?

In a global way : 在全球范围内:

    public class SpecificClass : BaseClass
    {
    }

    public class BaseClass
    {
    }

    public class TemplatedClass<T>
    {
    }

    static void Main(string[] args)
    {
        var templateInstance = new TemplatedClass<SpecificClass>();
        var @true = typeof (BaseClass).IsAssignableFrom(templateInstance.GetType().GetGenericArguments()[0]);

        var templateInstance2 = new TemplatedClass<int>();
        var @false = typeof (BaseClass).IsAssignableFrom(templateInstance2.GetType().GetGenericArguments()[0]);
    }

You can get the generic type arguments using Type.GetGenericArguments() . 您可以使用Type.GetGenericArguments()获得泛型类型参数。

Then your IsExceptionBoundToType method may look something like this: 然后,您的IsExceptionBoundToType方法可能看起来像这样:

public static bool IsExceptionBoundToType(FaultException fe, Type checkType)
{
    bool isBound = false;
    Type feType = fe.GetType();
    if (feType.IsGenericType && feType.GetGenericTypeDefinition() == typeof(FaultException<>))
    {
        Type faultType = feType.GetGenericArguments()[0];
        isBound = checkType.IsAssignableFrom(faultType);
    }

    return isBound;
}

As far as I can tell, there is no simple way to is-check a generic class; 据我所知,没有简单的方法来检查通用类。 likely due to the flexibility of the generic parameter. 可能是由于通用参数的灵活性。 Here is a solution: 这是一个解决方案:

public static bool IsExceptionBoundToType(FaultException fe, Type checkType)
{
   bool isBound = false;

   // Check to see if the FaultException is a generic type.
   Type feType = fe.GetType();
   if (feType.IsGenericType && feType.GetGenericTypeDefinition() == typeof(FaultException<>))
   {
      // Check to see if the Detail property is a class of the specified type.
      PropertyInfo detailProperty = feType.GetProperty("Detail");
      if (detailProperty != null)
      {
         object detail = detailProperty.GetValue(fe, null);
         isBound = checkType.IsAssignableFrom(detail.GetType());
      }
   }

   return (isBound);
}

Catch the exception and check it like this: 捕获异常并按以下方式检查它:

catch (Exception ex)
{
   if ((ex is FaultException) && IsExceptionBoundToType(ex, typeof(MyFaultBase)))
   {
      // do something
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通用方法类型不能用作通用类的通用类型 - Generic method type can't be used as generic type for generic class 在泛型类中使用枚举类型时,如何将枚举序列化为字符串? - How to serialize enum as string when the enum type is used in a generic class? .NET:如何检查泛型类中的类型? - .NET: How to check the type within a generic typed class? 检查FieldInfo类型是否是没有类型的泛型类 - Check if FieldInfo type is generic class without type 如何使用泛型where子句检查也具有泛型类型的类类型 - How to use the Generic where clause to check for Class Type that also has a Generic Type 访问基类中使用的泛型类型参数 - Accessing generic type parameters used in base class 如何在 C# 通用 class 中找到要在通用 function 中使用的基础类型? - How to find the underlying type in a C# generic class to be used in a generic function? 检查类型是否派生自抽象泛型类 - Check if type is derived from abstract generic class 如何指定类型参数,以便泛型基类将使用其调用的DAL方法所使用的相同模型类? - How to specify a type parameter so that a generic base class will use the same model class that is used by the DAL method that it calls? 可以将当前类的类型用作泛型类型参数的值吗? - Can the type of the current class be used as the value of a generic type parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM