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

C# 如何检查泛型类使用的类型,c#,generics,C#,Generics,考虑这样一种情况,即您拥有从MyFaultBase派生的各种类。因此,当web服务需要指示错误时,它会抛出类型为FaultException的异常 捕获此异常后,如何确定FaultException是否绑定到从MyFaultBase派生的类?据我所知,没有简单的方法检查泛型类;可能是由于通用参数的灵活性。以下是一个解决方案: public static bool IsExceptionBoundToType(FaultException fe, Type checkType) { bool

考虑这样一种情况,即您拥有从
MyFaultBase
派生的各种类。因此,当web服务需要指示错误时,它会抛出类型为
FaultException
的异常


捕获此异常后,如何确定
FaultException
是否绑定到从
MyFaultBase
派生的类?

据我所知,没有简单的方法检查泛型类;可能是由于通用参数的灵活性。以下是一个解决方案:

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

据我所知,没有简单的方法检查泛型类;可能是由于通用参数的灵活性。以下是一个解决方案:

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);
}
以全球方式:

    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]);
    }
公共类特定类:基类
{
}
公共类基类
{
}
公共类TemplatedClass
{
}
静态void Main(字符串[]参数)
{
var templateInstance=new TemplatedClass();
var@true=typeof(BaseClass).IsAssignableFrom(templateInstance.GetType().GetGenericArguments()[0]);
var templateInstance2=新的TemplatedClass();
var@false=typeof(BaseClass).IsAssignableFrom(templateInstance2.GetType().GetGenericArguments()[0]);
}
以全局方式:

    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]);
    }
公共类特定类:基类
{
}
公共类基类
{
}
公共类TemplatedClass
{
}
静态void Main(字符串[]参数)
{
var templateInstance=new TemplatedClass();
var@true=typeof(BaseClass).IsAssignableFrom(templateInstance.GetType().GetGenericArguments()[0]);
var templateInstance2=新的TemplatedClass();
var@false=typeof(BaseClass).IsAssignableFrom(templateInstance2.GetType().GetGenericArguments()[0]);
}

您可以使用获取泛型类型参数

然后您的
IsExceptionBoundToType
方法可能如下所示:

catch (Exception ex)
{
   if ((ex is FaultException) && IsExceptionBoundToType(ex, typeof(MyFaultBase)))
   {
      // do something
   }
}
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;
}
公共静态bool IsExceptionBoundToType(FaultException fe,类型checkType)
{
bool isBound=false;
类型feType=fe.GetType();
if(feType.IsGenericType&&feType.GetGenericTypeDefinition()==typeof(FaultException))
{
类型faultType=feType.GetGenericArguments()[0];
isBound=checkType.IsAssignableFrom(faultType);
}
返回绑定;
}

您可以使用获取泛型类型参数

然后您的
IsExceptionBoundToType
方法可能如下所示:

catch (Exception ex)
{
   if ((ex is FaultException) && IsExceptionBoundToType(ex, typeof(MyFaultBase)))
   {
      // do something
   }
}
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;
}
公共静态bool IsExceptionBoundToType(FaultException fe,类型checkType)
{
bool isBound=false;
类型feType=fe.GetType();
if(feType.IsGenericType&&feType.GetGenericTypeDefinition()==typeof(FaultException))
{
类型faultType=feType.GetGenericArguments()[0];
isBound=checkType.IsAssignableFrom(faultType);
}
返回绑定;
}