C#如何将变量用作对象中的属性名称列出键-值字符串数组的对象构造函数 [英] C# How to use variables as property names in an object List object constructor for key-value string arrays

查看:504
本文介绍了C#如何将变量用作对象中的属性名称列出键-值字符串数组的对象构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我已经解析了导入到对象中的键/值对.我这样做是因为我希望能够按逻辑上容易实现的名称属性进行排序:

I have parsed key-value pairs that I imported into objects. I did this because I want to be able to sort by the name properties which the logic is easy for:

var varDupe = variableList.GroupBy(v => v.varName).Where(vName => vName.Count() > 1).Select(vName => vName.Key);

我在这些字典和字典列表之间进行了辩论,但是最后似乎似乎更容易进行搜索,因为我不需要导出这些键值对,只需对它们进行错误检查即可.对于我的一些对象列表,我将需要很多依赖项,例如如果存在属性Q,则需要属性X,Y和Z",并立即搜索所有不同的对象.如果我希望使用字典列表更好,请告诉我.

I debated between these and a dictionary list, but in the end it seemed like searching was easier as I don't need to export these key-value pairs, only error-check them. I will need a lot of dependencies like "properties X,Y, and Z is required if property Q exists" for a few of my object lists and search through all of the distinct objects at once. If I would be better of with a list of dictionaries, please let me know.

我的代码现在可以正确地使用正确排序的键值对,但是作为键值对,它们本质上不需要按正确的顺序进行分组.我为解析的键值字符串数组创建了排序功能

My code works perfectly now with correctly sequenced key-value pairs, but as key value pairs, they inherently don't need to be grouped in the correct order. I made a sorting function for my parsed key-value string arrays

问题

该函数对字符串数组进行排序,但是当我缺少键时(例如,如果varVal是可选的),就会出现问题. varVal和varParam都应为双精度,所以我需要在对象中将它们转换为

That function sorts the string arrays, but the problem comes when I have missing keys (for example, if varVal is optional). varVal and varParam should both be doubles, so I need to convert them in the object

假设我有一个字符串数组,其中省略了一个可选参数.

Assume I have the string array leaving out an optional parameter.

varName,name1,varParam,1.01

完整"数组如下所示:

varName,name1,varVal,3.9,varParam,1.01

我具有用于构建对象的以下代码:

I have this code for building my objects:

 switch (sortedVals.Count())
                {
                    case 2:
                        materialList.Add(new Variable(sortedVals[1]));
                        break;
                    // Example of what I am trying to do to use the key-values to identify the correct property to assign a value to
                    case 4:
                        materialList.Add(new Variable(sortedVals[1]) { sortedVals[2] = Convert.ToDouble(sortedVals[3]) } );
                        break;
                    // Example of how it works correctly if they are all in order
                    case 6:
                        materialList.Add(new Variable(sortedVals[1], Convert.ToDouble(sortedVals[3]), Convert.ToDouble(sortedVals[5]) ) );
                        break;
                    ...

如果您查看情况4",则尝试使用字符串数组中的键"将正确的值"分配给它.我的Variable类中的Variable构造函数如下所示:

If you look at "case 4" I am trying to use the "key" from string array to assign the correct "value" to. My Variable constructor inside my Variable class looks like this:

public Variable(string varName, double varVal = 1, double varParam = 0)

sortedVals[2] = "varParam" 

编译器无法识别我的变量旨在用作构造函数名称的字符串(例如,情况4中的varParam = Convert.ToDouble("1.01")).

The compiler doesn't recognize that my variable is intended to be used as a string for the constructor name (i.e. varParam = Convert.ToDouble("1.01") in case 4).

如何在构造函数中使用像这样的字符串变量作为属性名称?

我会更好地报废这些对象并尝试创建字典列表并找到一种搜索名称重复项和其他属性的方法吗?

谢谢大家!

推荐答案

有多种方法可以实现:

这是一个非常基本的解决方案,没有linq等,并且假定您的拆分始终代表一个对象:

This is a very basic solution, no linq etc. and assuming your split is always representing a single object:

string name; 
double val = 1;
double val2 = 0;
for (int i = 0; i < sortedVals.length; i++)
{
    string key = sortedVals[i];
    i++;
    string valString = sortedVals.length > i ? sortedVals[i] : string.Empty;

    switch (key) 
    {
         case "varName":
             name = valString;
             break;
         case "varVal":
             val = valString != string.Empty ? double.Parse(valString) : val;
             break;
         case "varParam":
             val2 = valString != string.Empty ? double.Parse(valString) : val2;
             break;
    }
}
materialList.Add(new Variable(name, val, val2));

这篇关于C#如何将变量用作对象中的属性名称列出键-值字符串数组的对象构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆