altin
altin

Reputation: 1

defining a method dynamically in c#

I want that in the following method the else if block of code to be present based on the attrNameListCurrent.Length. In other words if attrNameListCurrent.Length=2 I need to have only if (attrNameListCurrent[0] != attrNameListPrevious[0]) and

else
 {
   result += AddItem(attrList[i]);
   if ( i == attrList.Count - 1)
     {
       result = result.TrimEnd(',');
       result += CloseObject(attrList[i]); ///
     }

part of code.

If attrNameListCurrent.Length=3 I want to add else if (attrNameListCurrent[1] != attrNameListPrevious[1]) block of code between them

if attrNameListCurrent.Length=4 I want to add else if (attrNameListCurrent[2] != attrNameListPrevious[2]) block of code and so forth.

Is any way to do this programmatically? Please any suggestion would be appreciated. My method:

public static string ReadRsdToJson(List<AttributeProperties> attrList)
 {
    string result = "{";
    bool createNew = true;
    string attrName = string.Empty;
    string[] attrNameListCurrent;
    string[] attrNameListPrevious;            
    if (attrList.Count == 0) result = "{}"; // Test -1; the list is empty
    for (var i = 0; i < attrList.Count; i++)
      {
        if (attrList[i].XPath != null)
          {
            if (i == 0) result += CreateNewObject(attrList[i]);
            if (i == 0 && i == attrList.Count - 1) result += CloseObject(attrList[i]);

            attrNameListCurrent = attrList[i].XPath.Split('/');
            if (i != 0)
              {
                attrNameListPrevious = attrList[i - 1].XPath.Split('/');                       
                if (attrNameListCurrent[0] != attrNameListPrevious[0])
                  {
                    result = result.TrimEnd(','); //close the previous object                            
                    if (attrNameListCurrent[0] != attrList[i-1].XPath.Split('/')[0]) result += CloseObject(attrList[i-1]);//create new object
                    result += CreateNewObject(attrList[i]);
                    if (i==attrList.Count-1)
                      {
                        result = result.TrimEnd(',');
                        result += CloseObject(attrList[i]); ///
                      }                                          
                  }
              else if (attrNameListCurrent[1] != attrNameListPrevious[1])
                {
                     result = result.TrimEnd(',');
                     if (attrNameListCurrent[1] != attrList[i - 1].XPath.Split('/')[1]) result += CloseNestedNthObject(attrList[i-1], 1);
                      result += CreateNestedNthElement(attrList[i], 1);
                      if (i == attrList.Count - 1)
                       {
                         result = result.TrimEnd(',');
                         result += CloseObject(attrList[i]);
                       } 
                 }
                 else if (attrNameListCurrent[2] != attrNameListPrevious[2])
                  {
                         result = result.TrimEnd(',');
                         if (attrNameListCurrent[2] != attrList[i - 1].XPath.Split('/')[2]) result += CloseNestedNthObject(attrList[i-1], 2);
                         result += CreateNestedNthElement(attrList[i], 2);
                         if (i == attrList.Count - 1)
                          {
                            result = result.TrimEnd(',');
                            result += CloseObject(attrList[i]);
                          }  
                  }
                  else if (attrNameListCurrent[3] != attrNameListPrevious[3])
                   {
                          result = result.TrimEnd(',');
                          if (attrNameListCurrent[3] != attrList[i - 1].XPath.Split('/')[3]) result += CloseNestedNthObject(attrList[i-1], 3);
                          result += CreateNestedNthElement(attrList[i], 3);
                          if (i == attrList.Count - 1)
                           {
                             result = result.TrimEnd(',');
                             result += CloseObject(attrList[i]);
                           } 
                  }
                  else
                  {
                          result += AddItem(attrList[i]);
                          if ( i == attrList.Count - 1)
                            {
                              result = result.TrimEnd(',');
                              result += CloseObject(attrList[i]); ///
                            }    
                         }
                      }            
                   }
                   else
                    {
                      if (attrList[i].Type == "integer" || attrList[i].Aggregate != null)
                    {
                      result += '"' + attrList[i].Name + '"' + ": " + attrList[i].Value + ',';
                    }
                    else
                    {
                      result += '"' + attrList[i].Name + '"' + ": " + '"' + attrList[i].Value + '"' + ',';
                    }
                  }
                  if (i == attrList.Count - 1)
                   {
                     result = result.TrimEnd(',');
                     result += '}';
                   }
                 }
                 return result;
               }

Upvotes: 0

Views: 75

Answers (2)

Ian Mercer
Ian Mercer

Reputation: 39277

If what you are trying to do is build a tree from a list of paths + values and then serialize that to JSON it would be easier to build the tree and then serialize it. Suppose you have a TreeNode class that stores a list of child TreeNodes and a Value. Suppose it supports an indexer so you can access the child nodes using []. Then you could do something like this:

public static string ReadRsdToJson(List<AttributeProperties> attrList)
{
  var root = new TreeNode();
  foreach (var attr in attrList)
  {
     var pathElements = attr.XPath.Split('/');
     var current = root;

     // Scan down the path following or building the tree at each step

     foreach (var pathElement in pathElements)
     {
        if (current.ContainsKey(pathElement))
          current = current[pathElement];
        else
        {
           var newChild = new TreeNode(pathElement);
           current.AddChild(newChild);
           current = newChild;
        }
     }

     // current is now the node at leaf of the tree that you want
     current.Value = attr.Value;
  }

  // And now you have built a tree you can serialize that to Json
  // If you must write your own serializer instead of using JSON.NET
  // consider using string.Join(",",...) instead of trimming commas
}

It doesn't match your code exactly but your code was really too long and complicated for a SO question. Best to simplify it down to the bare essence of what you are asking.

Upvotes: 0

Nick
Nick

Reputation: 1863

Have you though about using a switch statement? It sounds like it might be what you need if i am understanding correctly.

Something like

switch (attrNameListCurrent.Length)
        {
            case 1:
                #if length is 1 do thing
                break;
            case 2:
                #if length is 2 do thing
                break;
            case 3:
                #if length is 3 do thing
                break;
        }

Upvotes: 1

Related Questions