小编stu*_*e06的帖子

通用类型作为字典键的类型不起作用

我为我的 Web API 应用程序创建了一个自定义异常过滤器。该过滤器相当简单,并使用字典将异常类型映射到 http 状态代码。

\n\n
this.Mappings = new Dictionary<Type, HttpStatusCode>\n{\n    {typeof (ArgumentException), HttpStatusCode.BadRequest},\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我遇到的问题是我定义了一个通用异常类型:

\n\n
class EntityNotFoundException<TEntity> : RepositoryException { /*... */ }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想将此异常类型映射到HttpStatusCode.NotFound.

\n\n
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\n
throw 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\n

我看到两个选择:

\n\n
    \n
  1. 删除泛型并将 Type 参数添加到我的异常的构造函数中;
  2. \n
  3. 映射EntityNotFoundException<User>HttpStatusCode.NotFound,这将为我的每个实体创建一个映射。
  4. \n
\n\n

有没有更清洁的解决方案?

\n

.net c# asp.net-web-api

2
推荐指数
1
解决办法
158
查看次数

如何定义系统()

所以我想定义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)

在这种情况下不做任何事情.我究竟做错了什么?

c bash operating-system

1
推荐指数
1
解决办法
72
查看次数

可以创建任意类型的constexpr链接列表吗?

我正在尝试实现基于特征的策略子系统,但是我有一个我真的不知道该如何解决的问题(如果可能的话)。我的特征看起来像这样:

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)

c++ traits constexpr c++11

1
推荐指数
1
解决办法
79
查看次数

标签 统计

.net ×1

asp.net-web-api ×1

bash ×1

c ×1

c# ×1

c++ ×1

c++11 ×1

constexpr ×1

operating-system ×1

traits ×1