
类图如下:
这个类的作用是:Provides standard implementation for simple extendent data storage
这个类的核心是一个NameValueCollection类型的变量,存储一系列"名称-值"的列表。
以下是各个方法的功能分析:
1. GetExtendedAttribute(string name) 方法:根据key的名称获得值,如果找不到对应的值,就返回string.Empty.
public string GetExtendedAttribute(string name)
{
string returnValue = extendedAttributes[name];
if (returnValue == null)
return string.Empty;
else
return returnValue;
}
{
string returnValue = extendedAttributes[name];
if (returnValue == null)
return string.Empty;
else
return returnValue;
}
2. SetExtendedAttribute(string name, string value) 方法:设置对应name的value;当value为null或者string.Empty时,就清除name对应的项
public void SetExtendedAttribute(string name, string value)
{
if((value == null) || (value == string.Empty))
extendedAttributes.Remove(name);
else
extendedAttributes[name] = value;
}
{
if((value == null) || (value == string.Empty))
extendedAttributes.Remove(name);
else
extendedAttributes[name] = value;
}
3. ExtendedAttributesCount : 获得当前NameValueCollection类型变量的项数
public int ExtendedAttributesCount
{
get { return extendedAttributes.Count; }
}
{
get { return extendedAttributes.Count; }
}
4. 获取不同类型的键值:如果存在就返回值,如果不存在就返回指定的默认值
protected bool GetBool(string name, bool defaultValue)
{
string b = GetExtendedAttribute(name);
if(b == null || b.Trim().Length == 0)
return defaultValue;
return bool.Parse(b);
}
protected int GetInt(string name, int defaultValue)
{
string i = GetExtendedAttribute(name);
if(i == null || i.Trim().Length == 0)
return defaultValue;
return Int32.Parse(i);
}
protected string GetString(string name, string defaultValue)
{
string v = GetExtendedAttribute(name);
return (Globals.IsNullorEmpty(v)) ? defaultValue : v;
}
{
string b = GetExtendedAttribute(name);
if(b == null || b.Trim().Length == 0)
return defaultValue;
return bool.Parse(b);
}
protected int GetInt(string name, int defaultValue)
{
string i = GetExtendedAttribute(name);
if(i == null || i.Trim().Length == 0)
return defaultValue;
return Int32.Parse(i);
}
protected string GetString(string name, string defaultValue)
{
string v = GetExtendedAttribute(name);
return (Globals.IsNullorEmpty(v)) ? defaultValue : v;
}
5. GetSerializerData()方法:将当前NameValueCollection类型的变量序列化,也就是转换成SerializerData类型的变量
public SerializerData GetSerializerData()
{
SerializerData data = new SerializerData();
//data.Bytes = Serializer.ConvertToBytes(this.extendedAttributes);
string keys = null;
string values = null;
Serializer.ConvertFromNameValueCollection(this.extendedAttributes,ref keys, ref values);
data.Keys = keys;
data.Values = values;
return data;
}
{
SerializerData data = new SerializerData();
//data.Bytes = Serializer.ConvertToBytes(this.extendedAttributes);
string keys = null;
string values = null;
Serializer.ConvertFromNameValueCollection(this.extendedAttributes,ref keys, ref values);
data.Keys = keys;
data.Values = values;
return data;
}
6. SetSerializerData(SerializerData data)方法:将SerializerData类型变量转换成NameValueCollection类型的变量。
public void SetSerializerData(SerializerData data)
{
if(this.extendedAttributes == null || this.extendedAttributes.Count == 0)
{
this.extendedAttributes = Serializer.ConvertToNameValueCollection(data.Keys,data.Values);
}
if(this.extendedAttributes == null)
extendedAttributes = new NameValueCollection();
}
{
if(this.extendedAttributes == null || this.extendedAttributes.Count == 0)
{
this.extendedAttributes = Serializer.ConvertToNameValueCollection(data.Keys,data.Values);
}
if(this.extendedAttributes == null)
extendedAttributes = new NameValueCollection();
}
下面看一下NameValueCollection类型的变量要转换成的SerializerData到底是一个什么东东:
using System;
namespace CommunityServer.Components
{
/// <summary>
/// Container for get/set data from the CS datastore
/// </summary>
public struct SerializerData
{
[Obsolete]
public byte[] Bytes;
public string Keys;
public string Values;
[Obsolete]
public bool HasBytes
{
get{ return Bytes != null && Bytes.Length > 0;}
}
}
}
namespace CommunityServer.Components
{
/// <summary>
/// Container for get/set data from the CS datastore
/// </summary>
public struct SerializerData
{
[Obsolete]
public byte[] Bytes;
public string Keys;
public string Values;
[Obsolete]
public bool HasBytes
{
get{ return Bytes != null && Bytes.Length > 0;}
}
}
}
可以看到,SerializerData是一个结构,包含两个string类型的变量,从这里可以看出,NameValueCollection类型变量转换成SerializerData类型变量就是把一个key-value对的列表转换成两个字符串。
另外还要注意类中的一个标记[Obsolete]:
msdn的解释:
标记不应使用的程序实体。
[Obsolete(
message
)]
[Obsolete(
message,
iserror
)]
参数
message
string;理想情况下,是人工可读的关于项已过时的原因以及改为使用什么的解释。
iserror
bool;如果为 true,则编译器应将该项的使用视为错误。默认值为 false(编译器生成一个警告)。
应用于
任何允许属性的声明。
备注
Obsolete 属性是使用一次的属性。Obsolete 是 System.ObsoleteAttribute 的别名。
在程序中使用标记为 Obsolete 的实体时,编译器将发出错误或警告(取决于 iserror)并输出 message。
示例
// cs_attribute_obsolete.cs
// CS0619 expected
using System;
public class MyClass
{
[Obsolete("Don't use OldWay; use NewWay instead", true)]
static void OldWay( ) { Console.WriteLine("Silly me!"); }
static void NewWay( ) { Console.WriteLine("D'oh!"); }
public static void Main( )
{
OldWay( );
}
}
也就是说以后如果暂时不用某个成员的时候,就可以用这个标记,而不用再注释掉了[Obsolete(
message
)]
[Obsolete(
message,
iserror
)]
参数
message
string;理想情况下,是人工可读的关于项已过时的原因以及改为使用什么的解释。
iserror
bool;如果为 true,则编译器应将该项的使用视为错误。默认值为 false(编译器生成一个警告)。
应用于
任何允许属性的声明。
备注
Obsolete 属性是使用一次的属性。Obsolete 是 System.ObsoleteAttribute 的别名。
在程序中使用标记为 Obsolete 的实体时,编译器将发出错误或警告(取决于 iserror)并输出 message。
示例
// cs_attribute_obsolete.cs
// CS0619 expected
using System;
public class MyClass
{
[Obsolete("Don't use OldWay; use NewWay instead", true)]
static void OldWay( ) { Console.WriteLine("Silly me!"); }
static void NewWay( ) { Console.WriteLine("D'oh!"); }
public static void Main( )
{
OldWay( );
}
}
Serializer.ConvertFromNameValueCollection方法:
/// <summary>
/// Creates a the keys and values strings for the simple serialization based on a NameValueCollection
/// </summary>
/// <param name="nvc">NameValueCollection to convert</param>
/// <param name="keys">the ref string will contain the keys based on the key format</param>
/// <param name="values">the ref string will contain all the values of the namevaluecollection</param>
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
{
if(nvc == null || nvc.Count == 0)
return;
StringBuilder sbKey = new StringBuilder();
StringBuilder sbValue = new StringBuilder();
int index = 0;
foreach(string key in nvc.AllKeys)
{
if(key.IndexOf(':') != -1)
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
string v = nvc[key];
if(!Globals.IsNullorEmpty(v))
{
sbKey.AppendFormat("{0}:S:{1}:{2}:",key,index,v.Length);
sbValue.Append(v);
index += v.Length;
}
}
keys = sbKey.ToString();
values = sbValue.ToString();
}
}
/// Creates a the keys and values strings for the simple serialization based on a NameValueCollection
/// </summary>
/// <param name="nvc">NameValueCollection to convert</param>
/// <param name="keys">the ref string will contain the keys based on the key format</param>
/// <param name="values">the ref string will contain all the values of the namevaluecollection</param>
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
{
if(nvc == null || nvc.Count == 0)
return;
StringBuilder sbKey = new StringBuilder();
StringBuilder sbValue = new StringBuilder();
int index = 0;
foreach(string key in nvc.AllKeys)
{
if(key.IndexOf(':') != -1)
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
string v = nvc[key];
if(!Globals.IsNullorEmpty(v))
{
sbKey.AppendFormat("{0}:S:{1}:{2}:",key,index,v.Length);
sbValue.Append(v);
index += v.Length;
}
}
keys = sbKey.ToString();
values = sbValue.ToString();
}
}
Serializer.ConvertToNameValueCollection方法:
/// <summary>
/// Creates a NameValueCollection from two string. The first contains the key pattern and the second contains the values
/// spaced according to the kys
/// </summary>
/// <param name="keys">Keys for the namevalue collection</param>
/// <param name="values">Values for the namevalue collection</param>
/// <returns>A NVC populated based on the keys and vaules</returns>
/// <example>
/// string keys = "key1:S:0:3:key2:S:3:2:";
/// string values = "12345";
/// This would result in a NameValueCollection with two keys (Key1 and Key2) with the values 123 and 45
/// </example>
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
{
NameValueCollection nvc = new NameValueCollection();
if(keys != null && values != null && keys.Length > 0 && values.Length > 0)
{
char[] splitter = new char[1] { ':' } ;
string[] keyNames = keys.Split(splitter);
for (int i = 0; i < (keyNames.Length / 4); i++)
{
int start = int.Parse(keyNames[(i * 4) + 2], CultureInfo.InvariantCulture);
int len = int.Parse(keyNames[(i * 4) + 3], CultureInfo.InvariantCulture);
string key = keyNames[i * 4];
//Future version will support more complex types
if (((keyNames[(i * 4) + 1] == "S") && (start >= 0)) && (len > 0) && (values.Length >= (start + len)))
{
nvc[key] = values.Substring(start, len);
}
}
}
return nvc;
}
/// Creates a NameValueCollection from two string. The first contains the key pattern and the second contains the values
/// spaced according to the kys
/// </summary>
/// <param name="keys">Keys for the namevalue collection</param>
/// <param name="values">Values for the namevalue collection</param>
/// <returns>A NVC populated based on the keys and vaules</returns>
/// <example>
/// string keys = "key1:S:0:3:key2:S:3:2:";
/// string values = "12345";
/// This would result in a NameValueCollection with two keys (Key1 and Key2) with the values 123 and 45
/// </example>
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
{
NameValueCollection nvc = new NameValueCollection();
if(keys != null && values != null && keys.Length > 0 && values.Length > 0)
{
char[] splitter = new char[1] { ':' } ;
string[] keyNames = keys.Split(splitter);
for (int i = 0; i < (keyNames.Length / 4); i++)
{
int start = int.Parse(keyNames[(i * 4) + 2], CultureInfo.InvariantCulture);
int len = int.Parse(keyNames[(i * 4) + 3], CultureInfo.InvariantCulture);
string key = keyNames[i * 4];
//Future version will support more complex types
if (((keyNames[(i * 4) + 1] == "S") && (start >= 0)) && (len > 0) && (values.Length >= (start + len)))
{
nvc[key] = values.Substring(start, len);
}
}
}
return nvc;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架