C# Case语句关闭传入接口参数

C# Case语句关闭传入接口参数,c#,C#,我试图根据我的接口确定输入的类型,然后对该类型进行验证。验证根据输入的类型而不同 public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest) { switch (preAuthorizeRequest) { case } } 但是,要么我不能用一个接口来实现,要么我没有做一些可以让它工作的事情 如果我不能在一个看

我试图根据我的接口确定输入的类型,然后对该类型进行验证。验证根据输入的类型而不同

  public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest)
    {
        switch (preAuthorizeRequest)
        {
             case    
        }
    }
但是,要么我不能用一个接口来实现,要么我没有做一些可以让它工作的事情

如果我不能在一个看起来像的接口上这样做,可能是因为我认为switch需要一个具体的类型,那么我该怎么做呢?只是检查typeof的常规if语句

这是唯一的方法吗?

您可以使用以下方法:

public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest)
{
    if (preAuthorizeRequest is PaymentRequestClassA)
    {
         var classA = (PaymentRequestClassA)preAuthorizeRequest;
         // handle PaymentRequestTypeA
    }
    else if (preAuthorizeRequest is PaymentRequestClassB)
    {
         var classA = (PaymentRequestClassB)preAuthorizeRequest;
         // handle PaymentRequestTypeB
    }
}

您可以使用或类似于此:

public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest)
{
    if (preAuthorizeRequest is PaymentRequestClassA)
    {
         var classA = (PaymentRequestClassA)preAuthorizeRequest;
         // handle PaymentRequestTypeA
    }
    else if (preAuthorizeRequest is PaymentRequestClassB)
    {
         var classA = (PaymentRequestClassB)preAuthorizeRequest;
         // handle PaymentRequestTypeB
    }
}

您可以使用或类似于此:

public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest)
{
    if (preAuthorizeRequest is PaymentRequestClassA)
    {
         var classA = (PaymentRequestClassA)preAuthorizeRequest;
         // handle PaymentRequestTypeA
    }
    else if (preAuthorizeRequest is PaymentRequestClassB)
    {
         var classA = (PaymentRequestClassB)preAuthorizeRequest;
         // handle PaymentRequestTypeB
    }
}

您可以使用或类似于此:

public static bool RequestIsValid(IPaymentRequest preAuthorizeRequest)
{
    if (preAuthorizeRequest is PaymentRequestClassA)
    {
         var classA = (PaymentRequestClassA)preAuthorizeRequest;
         // handle PaymentRequestTypeA
    }
    else if (preAuthorizeRequest is PaymentRequestClassB)
    {
         var classA = (PaymentRequestClassB)preAuthorizeRequest;
         // handle PaymentRequestTypeB
    }
}


您必须使用
GetType
方法和
typeof()
,但不幸的是,您不能使用
开关
/
案例
来做出决定:

开关表达式或大小写标签必须是
bool
char
string
integral
enum
或相应的可空类型

您可以使用
if
/
else
语句或准备
字典
并使用它执行验证逻辑

private static Dictionary<Type, Func<IInterface, bool>> _validationFunctions
   = new Dictionary<Type, Func<IInterface, bool>>() {
       { typeof(ClassA), (input) => false },
       { typeof(ClassB), (input) => true }
   };

public static bool RequestIsValid(IInterface preAuthorizeRequest)
{
    return _validationFunctions[preAuthorizeRequest.GetType()](preAuthorizeRequest);
}
private static Dictionary\u validationFunctions
=新字典(){
{typeof(ClassA),(输入)=>false},
{typeof(ClassB),(输入)=>true}
};
公共静态bool请求有效(接口预授权请求)
{
返回_validationFunctions[preauthorizereRequest.GetType()](preauthorizereRequest);
}

您必须使用
GetType
方法和
typeof()
,但不幸的是,您不能使用
开关
/
案例
来做出决定:

开关表达式或大小写标签必须是
bool
char
string
integral
enum
或相应的可空类型

您可以使用
if
/
else
语句或准备
字典
并使用它执行验证逻辑

private static Dictionary<Type, Func<IInterface, bool>> _validationFunctions
   = new Dictionary<Type, Func<IInterface, bool>>() {
       { typeof(ClassA), (input) => false },
       { typeof(ClassB), (input) => true }
   };

public static bool RequestIsValid(IInterface preAuthorizeRequest)
{
    return _validationFunctions[preAuthorizeRequest.GetType()](preAuthorizeRequest);
}
private static Dictionary\u validationFunctions
=新字典(){
{typeof(ClassA),(输入)=>false},
{typeof(ClassB),(输入)=>true}
};
公共静态bool请求有效(接口预授权请求)
{
返回_validationFunctions[preauthorizereRequest.GetType()](preauthorizereRequest);
}

您必须使用
GetType
方法和
typeof()
,但不幸的是,您不能使用
开关
/
案例
来做出决定:

开关表达式或大小写标签必须是
bool
char
string
integral
enum
或相应的可空类型

您可以使用
if
/
else
语句或准备
字典
并使用它执行验证逻辑

private static Dictionary<Type, Func<IInterface, bool>> _validationFunctions
   = new Dictionary<Type, Func<IInterface, bool>>() {
       { typeof(ClassA), (input) => false },
       { typeof(ClassB), (input) => true }
   };

public static bool RequestIsValid(IInterface preAuthorizeRequest)
{
    return _validationFunctions[preAuthorizeRequest.GetType()](preAuthorizeRequest);
}
private static Dictionary\u validationFunctions
=新字典(){
{typeof(ClassA),(输入)=>false},
{typeof(ClassB),(输入)=>true}
};
公共静态bool请求有效(接口预授权请求)
{
返回_validationFunctions[preauthorizereRequest.GetType()](preauthorizereRequest);
}

您必须使用
GetType
方法和
typeof()
,但不幸的是,您不能使用
开关
/
案例
来做出决定:

开关表达式或大小写标签必须是
bool
char
string
integral
enum
或相应的可空类型

您可以使用
if
/
else
语句或准备
字典
并使用它执行验证逻辑

private static Dictionary<Type, Func<IInterface, bool>> _validationFunctions
   = new Dictionary<Type, Func<IInterface, bool>>() {
       { typeof(ClassA), (input) => false },
       { typeof(ClassB), (input) => true }
   };

public static bool RequestIsValid(IInterface preAuthorizeRequest)
{
    return _validationFunctions[preAuthorizeRequest.GetType()](preAuthorizeRequest);
}
private static Dictionary\u validationFunctions
=新字典(){
{typeof(ClassA),(输入)=>false},
{typeof(ClassB),(输入)=>true}
};
公共静态bool请求有效(接口预授权请求)
{
返回_validationFunctions[preauthorizereRequest.GetType()](preauthorizereRequest);
}

仅为了完整性:

如果不需要“默认”处理,还可以为IPaymentRequests的不同实现重载该方法,并删除将接口作为参数的方法。这也是更安全的,因为添加显然可能需要特殊处理的新实现并不是默认的,而是需要另一个具有匹配签名的方法

public static bool RequestIsValid(PaymentRequestImplementation1 preAuthorizeRequest)
{
}

public static bool RequestIsValid(PaymentRequestImplementation2 preAuthorizeRequest)
{
}

...
如果您确实想让该方法继续执行IRequest,您还可以使用反射来调用更具体的方法,以防调用方在IRequest中强制转换参数:

public static bool RequestIsValid(IRequest preAuthorizeRequest)
{
    var member = typeof (YourType).GetMethod(
        "RequestIsValid",
        BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
        null,
        new [] {preAuthorizeRequest.GetType()},
        null);

    if (member.GetParameters()[0].ParameterType != typeof (IRequest))
    {
        member.Invoke(null, new[] {Convert.ChangeType(preAuthorizeRequest, preAuthorizeRequest.GetType())});
    }

    // default
}

仅为了完整性:

如果不需要“默认”处理,还可以为IPaymentRequests的不同实现重载该方法,并删除将接口作为参数的方法。这也是更安全的,因为添加显然可能需要特殊处理的新实现并不是默认的,而是需要另一个具有匹配签名的方法

public static bool RequestIsValid(PaymentRequestImplementation1 preAuthorizeRequest)
{
}

public static bool RequestIsValid(PaymentRequestImplementation2 preAuthorizeRequest)
{
}

...
如果您确实想让该方法继续执行IRequest,您还可以使用反射来调用更具体的方法,以防调用方在IRequest中强制转换参数:

public static bool RequestIsValid(IRequest preAuthorizeRequest)
{
    var member = typeof (YourType).GetMethod(
        "RequestIsValid",
        BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
        null,
        new [] {preAuthorizeRequest.GetType()},
        null);

    if (member.GetParameters()[0].ParameterType != typeof (IRequest))
    {
        member.Invoke(null, new[] {Convert.ChangeType(preAuthorizeRequest, preAuthorizeRequest.GetType())});
    }

    // default
}

仅为了完整性:

如果不需要“默认”处理,还可以为IPaymentRequests的不同实现重载该方法,并删除将接口作为参数的方法。这也是更安全的,因为添加显然可能需要特殊处理的新实现并不是默认的,而是需要另一个具有匹配签名的方法

public static bool RequestIsValid(PaymentRequestImplementation1 preAuthorizeRequest)
{
}

public static bool RequestIsValid(PaymentRequestImplementation2 preAuthorizeRequest)
{
}

...
如果您确实想让该方法继续执行IRequest,您还可以使用反射来调用更具体的方法,以防调用方在IRequest中强制转换参数:

public static bool RequestIsValid(IRequest preAuthorizeRequest)
{
    var member = typeof (YourType).GetMethod(
        "RequestIsValid",
        BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
        null,
        new [] {preAuthorizeRequest.GetType()},
        null);

    if (member.GetParameters()[0].ParameterType != typeof (IRequest))
    {
        member.Invoke(null, new[] {Convert.ChangeType(preAuthorizeRequest, preAuthorizeRequest.GetType())});
    }

    // default
}

仅为了完整性:

您还可以为IPaymentReq的不同实现重载该方法