首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型“string”不能用作类型参数“TValue”,没有从“string”到“System.IFormattable”的隐式引用转换

类型“string”不能用作类型参数“TValue”,没有从“string”到“System.IFormattable”的隐式引用转换
EN

Stack Overflow用户
提问于 2021-10-03 05:06:35
回答 3查看 59关注 0票数 0

我有一个问题,这是我的代码片段

代码语言:javascript
复制
        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();
                }
            }
        }

下面是有问题的代码

代码语言:javascript
复制
          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]

有没有人能帮我写一下代码,是不是主要代码有问题,或者是法令我看了看,没有很好的答案。

以下是The reference of the game The nuget code的一些链接

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-10-04 23:37:28

代码语言:javascript
复制
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上,而不是整个类上。

票数 0
EN

Stack Overflow用户

发布于 2021-10-03 05:12:01

尝试使用FormattableString而不是string。参考:Difference between String, FormattableString, IFormattable

票数 1
EN

Stack Overflow用户

发布于 2021-10-03 06:11:55

看起来没问题。您是否使用了另一个ExtDictionary类(同名但不同的命名空间/模块)?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69422222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档