Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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#_Dictionary_Standards - Fatal编程技术网

C#强制使用字典键

C#强制使用字典键,c#,dictionary,standards,C#,Dictionary,Standards,我想知道什么是验证或强制用于Dictionary对象/参数的键的最佳实践 以这种方法为例: public override SqlServerQueryBuilder From(Dictionary<string, string> tableReferenceDictionary) { this.SelectQuery += " FROM " + this.IdentifierStart + tableReferenceD

我想知道什么是验证或强制用于Dictionary对象/参数的键的最佳实践

以这种方法为例:

public override SqlServerQueryBuilder From(Dictionary<string, string> tableReferenceDictionary)
        {

            this.SelectQuery += " FROM " + this.IdentifierStart + tableReferenceDictionary["Database"] + this.IdentifierEnd
                             + "." + this.IdentifierStart + tableReferenceDictionary["Schema"] + this.IdentifierEnd
                             + "." + this.IdentifierStart + tableReferenceDictionary["Table"] + this.IdentifierEnd;

            return this;
        }
public覆盖来自(字典表引用字典)的SqlServerQueryBuilder
{
this.SelectQuery+=“FROM”+this.IdentifierStart+tableReferenceDictionary[“Database”]+this.IdentifierEnd
+“+this.IdentifierStart+tableReferenceDictionary[“Schema”]+this.IdentifierEnd
+““+this.IdentifierStart+tableReferenceDictionary[“Table”]+this.IdentifierEnd;
归还这个;
}
此方法采用Dictionary参数,并对使用的键进行假设。如果调用方传递了一个输入错误(“数据库”而不是“数据库”),那么这段代码将抛出一个异常。有没有更好的管理方法

可能是实现相同功能的不同数据类型


可能使用枚举并使类型为enum(
Dictionary
)的键?

此处的自定义类更合适,因此您可以强制按预期填充所有“键”(将是新类中的属性)。

是的,您是正确的。使用
Dictionary
可能是最好的方法。

是的,我认为自定义类比Dictionary更合适。一个具有3个字段的构造函数的类可以说我有一个SqlQuerybuilder类为应用程序的其余部分构建Sql。该类是否应该是SqlQueryBuilder中的嵌套类?与SqlQueryBuilder.TableReferenceIf类似,如果它仅对SqlQueryBuilder有用,则嵌套类在这种情况下有效。