public class DynamicDictionary : DynamicDictionary<object>dynamic obj = new DynamicDictionary
{
["name"] = "Alice",
["age"] = 30,
["isAdmin"] = true,
["address"] = new DynamicDictionary
{
["city"] = "Xian",
["zip"] = "710000"
}
};
// 属性形式取值,不区分大小写
Console.WriteLine($"name = {obj.name}");
Console.WriteLine($"NAME = {obj.NAME}");
Console.WriteLine($"age = {obj.Age}");
Console.WriteLine($"Address.City = {obj.Address.City}");
// 属性形式设置值,不区分大小写
obj.Age = 40;
Console.WriteLine($"age = {obj.age}");
try
{
// 获取不存在的键值,会引发异常
_ = obj.City;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// 设置不存在的键值,会自动添加之
obj.City = "Xian";
Console.WriteLine($"city = {obj.city}");
// 也是字典
IDictionary<string, object> realDict = obj as IDictionary<string, object>;
foreach (var item in realDict)
Console.WriteLine($"{item.Key}: {item.Value}");
// 键不区分大小写
Console.WriteLine($"isAdmin = {realDict["isAdmin"]}");
Console.WriteLine($"ISADMIN = {realDict["ISADMIN"]}");
try
{
// "AGE" 不可添加,已有 "age" 键
realDict.Add("AGE", 50);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
DynamicDictionary dynamicDict = new DynamicDictionary
{
["name"] = "Alice",
["age"] = 30,
["isAdmin"] = true
};
// 若是已知 DynamicDictionary,可这样转 dynamic 去使用属性
dynamic obj2 = dynamicDict;
Console.WriteLine($"name = {obj2.name}");
Console.WriteLine($"has AGE = {dynamicDict.ContainsKey("AGE")}"); // true
Console.WriteLine($"has City = {dynamicDict.ContainsKey("City")}"); // false| DynamicDictionary | 构造新实例。 |
| DynamicDictionary(IDictionarystring, object) | 用现有字典构造新实例。 |
Public Properties | |
| Count | Gets the number of elements contained in the ICollectionT. (Inherited from DynamicDictionaryTVal) |
| IsReadOnly |
指示本集合是否为只读。
(Inherited from DynamicDictionaryTVal) |
| Item | Gets or sets the element with the specified key. (Inherited from DynamicDictionaryTVal) |
| Keys | Gets an ICollectionT containing the keys of the IDictionaryTKey, TValue. (Inherited from DynamicDictionaryTVal) |
| Values | Gets an ICollectionT containing the values in the IDictionaryTKey, TValue. (Inherited from DynamicDictionaryTVal) |
Public Methods | |
| Add(KeyValuePairstring, TVal) |
添加指定键值对中的内容。
(Inherited from DynamicDictionaryTVal) |
| Add(string, TVal) | Adds an element with the provided key and value to the IDictionaryTKey, TValue. (Inherited from DynamicDictionaryTVal) |
| Clear | Removes all items from the ICollectionT. (Inherited from DynamicDictionaryTVal) |
| Contains |
确定是否包含带有 指定键值对中的内容 的元素。
(Inherited from DynamicDictionaryTVal) |
| ContainsKey | Determines whether the IDictionaryTKey, TValue contains an element with the specified key. (Inherited from DynamicDictionaryTVal) |
| ContainsValue |
确定是否包含带有指定值的元素。
(Inherited from DynamicDictionaryTVal) |
| CopyTo | Copies the elements of the ICollectionT to an Array, starting at a particular Array index. (Inherited from DynamicDictionaryTVal) |
| CreateDynamicListFrom(IListDictionarystring, object) | 使用现有字典类列表,创建值类型为 object 的动态字典类列表。 |
| CreateDynamicListFrom(IListIDictionarystring, object) | 使用现有字典接口列表,创建值类型为 object 的动态字典类列表。 |
| CreateDynamicListFrom(Listobject) | 使用现有动态对象列表,创建值类型为 object 的动态字典类列表。 |
| GetEnumerator | Returns an enumerator that iterates through the collection. (Inherited from DynamicDictionaryTVal) |
| Remove(KeyValuePairstring, TVal) |
移除带有 指定键值对中的内容 的元素。
(Inherited from DynamicDictionaryTVal) |
| Remove(string) | Removes the element with the specified key from the IDictionaryTKey, TValue. (Inherited from DynamicDictionaryTVal) |
| ToString | Returns a string that represents the current object. (Inherited from DynamicDictionaryTVal) |
| TryGetMember | Provides the implementation for operations that get member values. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as getting a value for a property. (Inherited from DynamicDictionaryTVal) |
| TryGetValue | Gets the value associated with the specified key. (Inherited from DynamicDictionaryTVal) |
| TrySetMember | Provides the implementation for operations that set member values. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as setting a value for a property. (Inherited from DynamicDictionaryTVal) |
Protected Fields | |
| dictionary |
本类内部用来存储数据的字典实例,其键不区分大小写。
(Inherited from DynamicDictionaryTVal) |