mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2026-01-02 12:27:11 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
43
Infrastructure/Utilities/DynamicPropertyBag .cs
Normal file
43
Infrastructure/Utilities/DynamicPropertyBag .cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.IO;
|
||||
|
||||
namespace Infrastructure.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 动态属性Bag
|
||||
/// </summary>
|
||||
public class DynamicPropertyBag : DynamicObject
|
||||
{
|
||||
private Dictionary<string, object> storage = new Dictionary<string, object>();
|
||||
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
{
|
||||
if (storage.ContainsKey(binder.Name))
|
||||
{
|
||||
result = storage[binder.Name];
|
||||
return true;
|
||||
}
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool TrySetMember(SetMemberBinder binder, object value)
|
||||
{
|
||||
string key = binder.Name;
|
||||
if (storage.ContainsKey(key))
|
||||
storage[key] = value;
|
||||
else
|
||||
storage.Add(key, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringWriter message = new StringWriter();
|
||||
foreach (var item in storage)
|
||||
message.WriteLine("{0}:\t{1}", item.Key, item.Value);
|
||||
return message.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user