Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 属性解决方案中的字典_C#_.net_Reflection - Fatal编程技术网

C# 属性解决方案中的字典

C# 属性解决方案中的字典,c#,.net,reflection,C#,.net,Reflection,我有一个RESTWebAPI端点,它可以接收查询参数 其中一些查询参数用于生成具有内部模型属性的LINQ表达式。例如: http://api.example.com/scenes?episode=32 查询参数插曲生成LINQ表达式: s => s.Episode == 32 在每个查询参数具有的属性中指定用于比较的属性: public class SceneQueryData { [PropertyFilter("Episode")] public int? Ep

我有一个RESTWebAPI端点,它可以接收查询参数

其中一些查询参数用于生成具有内部模型属性的LINQ表达式。例如:

http://api.example.com/scenes?episode=32
查询参数
插曲
生成LINQ表达式:

s => s.Episode == 32
在每个查询参数具有的属性中指定用于比较的属性:

public class SceneQueryData
{
     [PropertyFilter("Episode")]
     public int? Episode { get; set; }
}
我是说:
“嘿,使用这个“插曲”查询参数作为模型的“插曲”属性的过滤器”

现在,生成的表达式是一个简单的等式比较(
=
),因此我需要更复杂的操作(
=
),因此我可以在属性中设置一个额外的成员:

[PropertyFilter("Episode", QueryOperations = new Dictionary<string, QueryOperation>()
{
    { "le", QueryOperation.LessThanOrEquals },
    { "lt", QueryOperation.LessThan },
    { "ge", QueryOperation.GreaterThanOrEquals },
    { "gt", QueryOperation.GreaterThan }
}]

但是,我无法将字典作为属性参数传递,因此我需要另一种方法来使用attributes来实现这一点,即使用AttributeUsage定义一个单独的属性(例如OperationAttribute),以便AllowMultiple=true。然后使用单独的OperationAttribute指定要放入字典中的每个键值对。像这样:

[AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true)]
public class OperationAttribute : Attribute 
{
    public OperationAttribute(string name, QueryOperation op)
    {
        ...etc...
    }
}

public class SceneQueryData
{
    [PropertyFilter("Episode")]
    [Operation("le", QueryOperation.LessThanOrEquals)]
    [Operation("lt", QueryOperation.LessThan)]
    [Operation("ge", QueryOperation.GreaterThanOrEquals)]
    [Operation("gt", QueryOperation.GreaterThan)]
    public int? Episode { get; set; }
}

使用AttributeUsage定义一个单独的属性(例如OperationAttribute),使AllowMultiple=true。然后使用单独的OperationAttribute指定要放入字典中的每个键值对。像这样:

[AttributeUsage(System.AttributeTargets.Property, AllowMultiple = true)]
public class OperationAttribute : Attribute 
{
    public OperationAttribute(string name, QueryOperation op)
    {
        ...etc...
    }
}

public class SceneQueryData
{
    [PropertyFilter("Episode")]
    [Operation("le", QueryOperation.LessThanOrEquals)]
    [Operation("lt", QueryOperation.LessThan)]
    [Operation("ge", QueryOperation.GreaterThanOrEquals)]
    [Operation("gt", QueryOperation.GreaterThan)]
    public int? Episode { get; set; }
}

您可以静态地定义从字符串到操作的映射,只需将允许的操作/字符串列表传递给属性?@Blorgbeard我想到了这一点,但是如果每个属性都想要一个不同的字符串->操作映射,那该怎么办呢?嗯,是吗?你在写代码。。从表面上看,这似乎不太必要。您可以静态地定义从字符串到操作的映射,并将允许的操作/字符串列表传递给属性?@Blorgbeard我想到了这一点,但如果每个属性都希望有不同的字符串->操作映射,该怎么办?好吧,是吗?你在写代码。。从表面上看,这对我来说似乎不太必要。