我为我的 Web API 应用程序创建了一个自定义异常过滤器。该过滤器相当简单,并使用字典将异常类型映射到 http 状态代码。
\n\nthis.Mappings = new Dictionary<Type, HttpStatusCode>\n{\n {typeof (ArgumentException), HttpStatusCode.BadRequest},\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n我遇到的问题是我定义了一个通用异常类型:
\n\nclass EntityNotFoundException<TEntity> : RepositoryException { /*... */ }\n
Run Code Online (Sandbox Code Playgroud)\n\n我想将此异常类型映射到HttpStatusCode.NotFound
.
this.Mappings = new Dictionary<Type, HttpStatusCode>\n{\n {typeof (ArgumentException), HttpStatusCode.BadRequest},\n {typeof (EntityNotFoundException<>), HttpStatusCode.NotFound},\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n但是,由于泛型在编译时生成多个类,因此 EntityNotFoundException 的任何实例实际上都不与我的映射的键匹配。
\n\n例如,如果我这样做:
\n\nthrow new EntityNotFoundException<User>(userId);\n
Run Code Online (Sandbox Code Playgroud)\n\n过滤器确实捕获了异常,但是 \xc2\xad\xc2\xad\xc2\xad\xc2\xadtypeof(EntityNotFoundException<>)\xc2\xad\xc2\xad\xc2\xad\xc2\xad
并且typeof(EntityNotFoundException<User>)
不会生成相同的哈希值。
我看到两个选择:
\n\nEntityNotFoundException<User>
到HttpStatusCode.NotFound
,这将为我的每个实体创建一个映射。有没有更清洁的解决方案?
\n所以我想定义system()函数!这是我的功能:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void mySystem (char *command)
{
execlp (command, command, (char*) 0);
}
int main (int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
char command[50];
strcpy(command, argv[i]);
mySystem(command);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试它就像那样:
gcc exe6.c;
./a.out ls ls
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它只做一个ls.
./a.out "ls -l"
Run Code Online (Sandbox Code Playgroud)
在这种情况下不做任何事情.我究竟做错了什么?
我正在尝试实现基于特征的策略子系统,但是我有一个我真的不知道该如何解决的问题(如果可能的话)。我的特征看起来像这样:
template <typename ValueT, typename TagT = void, typename EnableT = void>
struct TPolicyTraits
{
static void Apply(ValueT& value) { }
};
Run Code Online (Sandbox Code Playgroud)
这个特征可以这样专门化:
struct MyPolicy {};
template <typename ValueT>
struct TPolicyTraits<ValueT, MyPolicy>
{
static void Apply(ValueT& value) { /* Implementation */ }
};
Run Code Online (Sandbox Code Playgroud)
我想在编译时以某种链表的形式注册策略。策略系统将像这样使用:
namespace PolicyTraits
{
template <typename ValueT, typename TagT>
using TPolicyTraitsOf = TPolicyTraits<std::decay_t<ValueT>, TagT>;
template <typename ValueT>
void Apply(ValueT&& value)
{
// todo iterate through constexpr tag list and apply policies
}
template <typename TagT>
constexpr void …
Run Code Online (Sandbox Code Playgroud)