繁体   English   中英

如何在C#中将值添加到类型为object的变量中

[英]How to add values into a variable of type object in C#

现在我是实际的代码,成功地调试(运行)了:

object attributes = new object[] { 
                  "key1=value1",
                  "key2=value2",
                  "key3=value3",
                  "key4=value4",
                  "keyN=valueN" 
};

有时我需要更改值,所以我使用的C#词典定义为:

public Dictionary<string,string> dAttributes=new Dictionary<string,string>();

我正在字典中一一添加KEY和VALUE。 但是,当我尝试进行类型转换(我认为这是不可能的)或应用任何其他逻辑时,名为“属性”的对象中的值未采用正确的格式。

首先,我尝试了这个:

object attributes = new object[]{dAttributes};

然后我这样做:

            int i=0;
            string attribueKeyAndValues;
            attribueKeyAndValues = "";
            foreach (KeyValuePair<string,string> item in dAttributes)
            {
                if(i==(dealAttributes.Count-1))
                    attribueKeyAndValues+="\"" +item.Key+"="+item.Value+"\"";
                else
                    attribueKeyAndValues += "\"" + item.Key +"="+ item.Value + "\"" + ",";
                i++;
            }

            attributes = new object[] { attribueKeyAndValues };

现在,此代码不起作用,因为attribute将整个字符串作为单个值。 另外,更重要的是,当我调试代码并快速查看attribueKeyAndValues (在Text Visualizer中)中的值时,显示的是"\\key=value\\","\\key=value\\"等等。

还有其他方法可以在attribute添加值吗?

从字典到数组的转换

attributes = dAttributes.Select(a=>a.Key+"="+a.Value).ToArray();

这里使用协变量数组转换( ToArray()返回string[] ,但可以将其分配给object[]变量)

但是,如果您确实需要object[] ,请进行强制转换

attributes = dAttributes.Select(a=>(object)(a.Key+"="+a.Value)).ToArray();

然后像attributes[0] = 1; 将起作用(在第一种会引发运行时异常的方法中)

看起来您想将键和值从字典复制到字符串数组中。 您的最后一个代码示例创建了一个字符串,但稍作修改即可解决:

int i=0;
var attributes = new object[dAttributes.Count];
string attribueKeyAndValues = "";
foreach (KeyValuePair<string,string> item in dAttributes)
{
    result[i] = item.Key + "=" + item.Value;
    i++;
}

顺便说一句,您最好使用字符串数组而不是对象数组:

int i=0;
var attributes = new string[dAttributes.Count];
string attribueKeyAndValues = "";
foreach (KeyValuePair<string,string> item in dAttributes)
{
    result[i] = item.Key + "=" + item.Value;
    i++;
}

最后,您可以使用LINQ执行此操作:

var attributes = dAttributes.Select(item => item.Key + "=" + item.Value).ToArray();

或者,如果您确实需要将其用作对象数组而不是字符串数组,则可以执行以下操作:

var attributes = dAttributes.Select(item => item.Key + "=" + item.Value).ToArray<object>();

您可能想看一下动态对象,可以在其中添加任何所需的对象(当然,要注意一下)。

您可以在这里找到更多关于它们的信息

然后,您可以执行以下操作:

dynamic sampleObject = new ExpandoObject();
sampleObject.number = 10;
sampleObject.Increment = (Action)(() => { sampleObject.number++; });

好的,借助ASH的答案,我与我的另一个朋友成功地将其解决了,

public static class AttributeExtensions
    {
        public static object ToAttributeArray(this DAttributes dealAttr)
        {
            var objectsColl = dealAttr.dAttributes.Select(x => x.Key + "=" + x.Value);

            var objArray = objectsColl.Cast<object>();
            return  objArray.ToArray<object>();

        }
    }

现在它可以正常工作了,谢谢:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM