我有一个问题,这是我的代码片段
using System;
using System.IO;
using CsvHelper;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Collections;
using System.Globalization;
using CsvHelper.Configuration;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MXPSQL.EDict{
public class CvsStuff{
public CsvConfiguration cvscfg = new CsvConfiguration(CultureInfo.InvariantCulture)
{
NewLine = Environment.NewLine,
PrepareHeaderForMatch = args => args.Header.ToLower(),
Comment = '#'
};
}
// extensible dictionary
public class ExtDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
// original code
/*
public CloneableDictionary<TKey, TValue> Clone()
{
CloneableDictionary<TKey, TValue> clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> kvp in this)
{
clone.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return clone;
}
*/
public readonly CvsStuff cvss = new CvsStuff();
// new one
public ExtDictionary<TKey, TValue> New()
{
ExtDictionary<TKey, TValue> clone = new ExtDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> kvp in this)
{
clone.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return clone;
}
// extended code
public ExtDictionary<TKey, TValue> Clone(){
return New();
}
// convert from other to this
public void ConvertFromDictionary(Dictionary<TKey, TValue> dict){
foreach (KeyValuePair<TKey, TValue> kvp in dict){
this.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
}
public void ConvertFromKeyValuePair(KeyValuePair<TKey, TValue> kvpx){
var list = new List<KeyValuePair<TKey, TValue>>{kvpx};
var dict = list.ToDictionary(x => x.Key, x => x.Value);
// dict.Add(kvpx.Key, kvpx.Value.Clone());
/* foreach (KeyValuePair<TKey, TValue> kvp in kvpx){
this.Add(kvp.Key, (TValue) kvp.Value.Clone());
} */
ConvertFromDictionary(dict);
}
public void ConvertFromJson(string json){
Dictionary<TKey, TValue> dict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(json);
ConvertFromDictionary(dict);
}
/* public void ConvertFromCsv(string csvtr){
string res = "";
IEnumerable<T> IEBuffer;
ILookup<string, Order> Lookup;
using(var sreader = new StringReader(res))
using (var csv = new CsvReader(sreader, cvss.cvscfg))
{
// csv.WriteRecords(this);
IEBuffer = csv.GetRecords<T>;
Lookup = IEBuffer.ToLookup(o => o.CustomerName);
}
this = Lookup.ToDictionary(g => g.Key);
} */
/* public void ConvertFromCvs(string cvstr){
ConvertFromCsv(cvstr);
} */
// convert from this to others
public Dictionary<TKey, TValue> ToDictionary(){
Dictionary<TKey, TValue> NewDict = new Dictionary<TKey, TValue>();
ExtDictionary<TKey, TValue> edict = this.New();
foreach(KeyValuePair<TKey, TValue> kvp in edict){
NewDict.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return NewDict;
}
public List<KeyValuePair<TKey, TValue>> ToLKVP(){
var list = this.ToList<KeyValuePair<TKey, TValue>>();
return list;
}
public string ToJson(){
var strs = JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return strs;
}
public string ToCsv(){
string res = "";
using(var swriter = new StringWriter()){
using (var csv = new CsvWriter(swriter, cvss.cvscfg))
{
csv.WriteRecords(this);
}
res = swriter.ToString();
}
return res;
}
public string ToCvs(){
return ToCsv();
}
public string ToXML(){
string json = ToJson();
XNode node = JsonConvert.DeserializeXNode(json, "Root");
return node.ToString();
}
public string ToXml(){
return ToXML();
}
}
}下面是有问题的代码
static ExtDictionary<string, string> TemplaeDict = new ExtDictionary<string, string>();
static ExtDictionary<string, string> JSONDict = TemplaeDict.New();当运行"dotnet build“时,我得到这个错误。
C:\snek\Program.cs(151,40): error CS0311: The type 'string' cannot be used as type parameter 'TValue' in the generic type or method 'ExtDictionary<TKey, TValue>'. There is no implicit reference conversion from 'string' to 'System.IFormattable'. [C:\snek\snek.csproj]
C:\snek\Program.cs(152,40): error CS0311: The type 'string' cannot be used as type parameter 'TValue' in the generic type or method 'ExtDictionary<TKey, TValue>'. There is no implicit reference conversion from 'string' to 'System.IFormattable'. [C:\snek\snek.csproj]
有没有人能帮我写一下代码,是不是主要代码有问题,或者是法令我看了看,没有很好的答案。
发布于 2021-10-04 23:37:28
using System;
using System.IO;
using CsvHelper;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Collections;
using System.Globalization;
using CsvHelper.Configuration;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MXPSQL.EDict{
public class CvsStuff{
public CsvConfiguration cvscfg = new CsvConfiguration(CultureInfo.InvariantCulture)
{
NewLine = Environment.NewLine,
PrepareHeaderForMatch = args => args.Header.ToLower(),
Comment = '#'
};
}
// extensible dictionary
public class ExtDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IFormattable where TValue : ICloneable
{
// original code
/*
public CloneableDictionary<TKey, TValue> Clone()
{
CloneableDictionary<TKey, TValue> clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> kvp in this)
{
clone.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return clone;
}
*/
public readonly CvsStuff cvss = new CvsStuff();
// new one
public ExtDictionary<TKey, TValue> New()
{
ExtDictionary<TKey, TValue> clone = new ExtDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> kvp in this)
{
clone.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return clone;
}
// extended code
public ExtDictionary<TKey, TValue> Clone(){
return New();
}
// convert from other to this
public void ConvertFromDictionary(Dictionary<TKey, TValue> dict){
foreach (KeyValuePair<TKey, TValue> kvp in dict){
this.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
}
public void ConvertFromKeyValuePair(KeyValuePair<TKey, TValue> kvpx){
var list = new List<KeyValuePair<TKey, TValue>>{kvpx};
var dict = list.ToDictionary(x => x.Key, x => x.Value);
// dict.Add(kvpx.Key, kvpx.Value.Clone());
/* foreach (KeyValuePair<TKey, TValue> kvp in kvpx){
this.Add(kvp.Key, (TValue) kvp.Value.Clone());
} */
ConvertFromDictionary(dict);
}
public void ConvertFromJson(string json){
Dictionary<TKey, TValue> dict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(json);
ConvertFromDictionary(dict);
}
/* public void ConvertFromCsv(string csvtr){
string res = "";
IEnumerable<T> IEBuffer;
ILookup<string, Order> Lookup;
using(var sreader = new StringReader(res))
using (var csv = new CsvReader(sreader, cvss.cvscfg))
{
// csv.WriteRecords(this);
IEBuffer = csv.GetRecords<T>;
Lookup = IEBuffer.ToLookup(o => o.CustomerName);
}
this = Lookup.ToDictionary(g => g.Key);
} */
/* public void ConvertFromCvs(string cvstr){
ConvertFromCsv(cvstr);
} */
// convert from this to others
public Dictionary<TKey, TValue> ToDictionary(){
Dictionary<TKey, TValue> NewDict = new Dictionary<TKey, TValue>();
ExtDictionary<TKey, TValue> edict = this.New();
foreach(KeyValuePair<TKey, TValue> kvp in edict){
NewDict.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return NewDict;
}
public List<KeyValuePair<TKey, TValue>> ToLKVP(){
var list = this.ToList<KeyValuePair<TKey, TValue>>();
return list;
}
public string ToJson(){
var strs = JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
return strs;
}
public string ToCsv(){
string res = "";
using(var swriter = new StringWriter()){
using (var csv = new CsvWriter(swriter, cvss.cvscfg))
{
csv.WriteRecords(this);
}
res = swriter.ToString();
}
return res;
}
public string ToCvs(){
return ToCsv();
}
public string ToString(string? format, IFormatProvider? formatProvider){
return ToCvs();
}
public string ToXML(){
string json = ToJson();
XNode node = JsonConvert.DeserializeXNode(json, "Root");
return node.ToString();
}
public string ToXml(){
return ToXML();
}
}
}一个非常简单的解决方法,只需将I格式表移到where之前。问题是iformattable放在TValue上,而不是整个类上。
发布于 2021-10-03 05:12:01
尝试使用FormattableString而不是string。参考:Difference between String, FormattableString, IFormattable
发布于 2021-10-03 06:11:55
看起来没问题。您是否使用了另一个ExtDictionary类(同名但不同的命名空间/模块)?
https://stackoverflow.com/questions/69422222
复制相似问题