EmbeddedResources.CopyTo
This commit is contained in:
parent
11e5148cd2
commit
d7aa28a65d
@ -1,17 +0,0 @@
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public class DtsodBuilder
|
||||
{
|
||||
private StringBuilder b = new();
|
||||
|
||||
public DtsodBuilder AddProperty(string key, dynamic value)
|
||||
{
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public string BuildToString() => b.ToString();
|
||||
|
||||
public DtsodV23 Build() => new DtsodV23(BuildToString());
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
|
||||
public abstract class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
|
||||
{
|
||||
// да, вместо собственной реализации интерфейса это ссылки на Dictionary
|
||||
readonly Dictionary<TKey, TVal> baseDict;
|
||||
|
||||
public DtsodDict() => baseDict = new();
|
||||
public DtsodDict(IDictionary<TKey, TVal> srcDict) => baseDict = new(srcDict);
|
||||
protected DtsodDict() => baseDict = new();
|
||||
protected DtsodDict(IDictionary<TKey, TVal> srcDict) => baseDict = new(srcDict);
|
||||
|
||||
|
||||
public virtual TVal this[TKey key]
|
||||
@ -20,19 +20,13 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
|
||||
|
||||
public virtual bool TryGetValue(TKey key, out TVal value) => baseDict.TryGetValue(key, out value);
|
||||
|
||||
public virtual bool TrySetValue(TKey key, TVal value)
|
||||
{
|
||||
if (ContainsKey(key))
|
||||
public bool TrySetValue(TKey key, TVal value)
|
||||
{
|
||||
if (!ContainsKey(key))
|
||||
return false;
|
||||
baseDict[key] = value;
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
public virtual void Append(ICollection<KeyValuePair<TKey, TVal>> anotherDtsod)
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TVal> pair in anotherDtsod)
|
||||
Add(pair.Key, pair.Value);
|
||||
|
||||
}
|
||||
|
||||
public virtual void Add(TKey key, TVal value) => baseDict.Add(key, value);
|
||||
@ -40,6 +34,16 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
|
||||
public virtual void Add(KeyValuePair<TKey, TVal> pair)
|
||||
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Add(pair);
|
||||
|
||||
public void Add(object key, object value)
|
||||
{
|
||||
((IDictionary) baseDict).Add(key, value);
|
||||
}
|
||||
|
||||
public void Append(IEnumerable<KeyValuePair<TKey, TVal>> anotherDtsod)
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TVal> pair in anotherDtsod)
|
||||
Add(pair);
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
@ -64,11 +68,6 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
|
||||
set => ((IDictionary) baseDict)[key] = value;
|
||||
}
|
||||
|
||||
public void Add(object key, object value)
|
||||
{
|
||||
((IDictionary) baseDict).Add(key, value);
|
||||
}
|
||||
|
||||
public virtual void Clear() => baseDict.Clear();
|
||||
public bool Contains(object key)
|
||||
{
|
||||
|
||||
@ -13,9 +13,9 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
||||
public DtsodVersion Version { get; } = DtsodVersion.V23;
|
||||
public IDictionary<string, dynamic> ToDictionary() => this;
|
||||
|
||||
public DtsodV23() : base() {}
|
||||
public DtsodV23() {}
|
||||
public DtsodV23(IDictionary<string, dynamic> dict) : base(dict) {}
|
||||
public DtsodV23(string serialized) => Append(Deserialize(serialized));
|
||||
public DtsodV23(string serialized) => this.Append(Deserialize(serialized));
|
||||
|
||||
static DtsodV23 Deserialize(string _text)
|
||||
{
|
||||
@ -213,7 +213,7 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
object value = null;
|
||||
@ -296,9 +296,9 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
|
||||
};
|
||||
|
||||
protected StringBuilder Serialize(DtsodV23 dtsod, ref int tabscount, StringBuilder b = null)
|
||||
{;
|
||||
{
|
||||
tabscount++;
|
||||
if (b is null) b = new StringBuilder();
|
||||
b ??= new StringBuilder();
|
||||
foreach (var pair in dtsod)
|
||||
{
|
||||
b.Append('\t', tabscount).Append(pair.Key).Append(": ");
|
||||
|
||||
@ -5,14 +5,18 @@ namespace DTLib.Filesystem;
|
||||
|
||||
public static class EmbeddedResources
|
||||
{
|
||||
public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) =>
|
||||
(assembly ?? Assembly.GetCallingAssembly())
|
||||
.GetManifestResourceStream(resourcePath)
|
||||
?? throw new Exception($"embedded resource <{resourcePath}> not found");
|
||||
public static Stream GetResourceStream(string resourcePath, Assembly assembly = null)
|
||||
{
|
||||
assembly ??= Assembly.GetCallingAssembly();
|
||||
return assembly.GetManifestResourceStream(resourcePath)
|
||||
?? throw new Exception($"embedded resource <{resourcePath}> not found in assembly {assembly.FullName}");
|
||||
}
|
||||
|
||||
public static byte[] ReadBynary(string resourcePath, Assembly assembly = null)
|
||||
{
|
||||
if (assembly is null) assembly = Assembly.GetCallingAssembly();
|
||||
// is required to get the Assembly called ReadBynary
|
||||
// otherwise Assembly.GetCallingAssembly() in GetResourceStream will get DTLib assembly always
|
||||
assembly ??= Assembly.GetCallingAssembly();
|
||||
using var reader = new BinaryReader(GetResourceStream(resourcePath, assembly));
|
||||
return reader.ReadBytes(int.MaxValue);
|
||||
}
|
||||
@ -20,8 +24,15 @@ public static class EmbeddedResources
|
||||
|
||||
public static string ReadText(string resourcePath, Assembly assembly = null)
|
||||
{
|
||||
if (assembly is null) assembly = Assembly.GetCallingAssembly();
|
||||
assembly ??= Assembly.GetCallingAssembly();
|
||||
using var reader = new StreamReader(GetResourceStream(resourcePath, assembly));
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
public static void CopyToFile(string resourcePath, string filePath, Assembly assembly = null)
|
||||
{
|
||||
assembly ??= Assembly.GetCallingAssembly();
|
||||
using var file = File.OpenWrite(filePath);
|
||||
GetResourceStream(resourcePath, assembly).FluentCopyTo(file).Close();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user