Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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语言中字符串的往返安全转义#_C#_Mono_Escaping - Fatal编程技术网

C# C语言中字符串的往返安全转义#

C# C语言中字符串的往返安全转义#,c#,mono,escaping,C#,Mono,Escaping,我对C#中字符串的所有不同转义机制感到困惑。我想要的是一种逃逸/不逃逸的方法: 1) 可用于任何字符串 2) escape+unescape保证返回初始字符串 3) 用其他符号替换所有标点符号。如果要求太多,那么至少逗号、大括号和@。我对没有逃逸的空间很满意。 4) 不太可能改变 它存在吗 编辑:这是为了序列化和反序列化应用程序生成的属性。因此,我的对象可能有也可能没有Attribute1、Attribute2、Attribute3等的值。稍微简化一下,我们的想法是执行如下操作。目标是使编码的集

我对C#中字符串的所有不同转义机制感到困惑。我想要的是一种逃逸/不逃逸的方法:

1) 可用于任何字符串
2) escape+unescape保证返回初始字符串 3) 用其他符号替换所有标点符号。如果要求太多,那么至少逗号、大括号和@。我对没有逃逸的空间很满意。
4) 不太可能改变

它存在吗

编辑:这是为了序列化和反序列化应用程序生成的属性。因此,我的对象可能有也可能没有Attribute1、Attribute2、Attribute3等的值。稍微简化一下,我们的想法是执行如下操作。目标是使编码的集合简短,并且或多或少具有人类可读性

我想问的是,使用什么样的方法来逃生和逃逸是有意义的

public abstract class GenericAttribute {
  const string key1 = "KEY1";  //It is fine to put some restrictions on the keys, i.e. no punctuation
  const string key2 = "KEY2";
  public abstract string Encode(); // NO RESTRICTIONS ON WHAT ENCODE MIGHT RETURN
  public static GenericAttribute FromKeyValuePair (string key, string value) {
    switch (key) {
      case key1: return new ConcreteAttribute1(value);
      case key2: return new ConcreteAttribute2(value);
        // etc.
    }
  }
}

public class AttributeCollection {
  Dictionary <string, GenericAttribute> Content {get;set;}
  public string Encode() {
    string r = "";
    bool first = true;
    foreach (KeyValuePair<string, GenericAttribute> pair in this.Content) {
      if (first) {
        first = false;
      } else {
        r+=",";
      }
      r+=(pair.Key + "=" + Escape(pair.Value.Encode()));
    }
    return r;
  }

  public AttributeCollection(string encodedCollection) {
    // input string is the return value of the Encode method
    this.Content = new Dictionary<string, GenericAttribute>();
    string[] array = encodedCollection.Split(',');
    foreach(string component in array) {
      int equalsIndex = component.IndexOf('=');
      string key = component.Substring(0, equalsIndex);
      string value = component.Substring(equalsIndex+1);
      GenericAttribute attribute = GenericAttribute.FromKeyValuePair(key, Unescape(value));
      this.Content[key]=attribute;
    }
  }
}
公共抽象类GenericAttribute{
const string key1=“key1”//可以对键进行一些限制,即不使用标点符号
常量字符串key2=“key2”;
公共抽象字符串Encode();//对Encode可能返回的内容没有限制
来自KeyValuePair的公共静态GenericAttribute(字符串键、字符串值){
开关(钥匙){
案例键1:返回新属性1(值);
案例键2:返回新的ConcreteAttribute2(值);
//等等。
}
}
}
公共类属性集合{
字典内容{get;set;}
公共字符串编码(){
字符串r=“”;
bool first=true;
foreach(此.Content中的KeyValuePair对){
如果(第一){
第一个=假;
}否则{
r+=“,”;
}
r+=(pair.Key+“=”+转义(pair.Value.Encode());
}
返回r;
}
公共属性集合(字符串编码集合){
//输入字符串是Encode方法的返回值
this.Content=newdictionary();
字符串[]数组=encodedCollection.Split(',');
foreach(数组中的字符串组件){
int equalindex=component.IndexOf('=');
string key=component.Substring(0,equalsIndex);
字符串值=组件.子字符串(equalsIndex+1);
GenericAttribute属性=GenericAttribute.FromKeyValuePair(键,Unescape(值));
this.Content[key]=属性;
}
}
}

我不完全确定您的要求,但我相信您的意图是包含转义字符,即使是转义字符

var content = @"\'Hello";
Console.WriteLine(content);

// Output:
\'Hello
通过使用
@
它将包括所述转义,使其成为
字符串的一部分。这是针对使用C#的服务器端,用于解释其他语言和转义格式,只有您知道这一点

你可以在这里找到一些关于C#逃跑的好信息:


尝试使用
HttpServerUtility.UrlEncode
HttpServerUtility.UrlDecode
。我认为这将编码和解码所有你想要的东西。
请参阅,这里是上的映射说明。

简而言之,否。转义取决于字符串存储的格式(c#代码文件、XML、HTML、JavaScript等),因此没有一种动物可以转义/取消转义每种格式。有时一个字符串可以转义为多种格式,但只有您知道应用了哪种转义,因此只有您才能按正确的顺序取消转义所有内容。