Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace StringParameters
  5. {
  6.     // класс-пример
  7.     // public int A { get; set; } - свойство
  8.     // public int A; - поле
  9.     // в работе разницы никакой, в строчке 23 (var fields = ...)
  10.     // GetFields() выберет второй вариант,
  11.     // если написать GetProperties() - первый
  12.     class Foo
  13.     {
  14.         public int A;
  15.         public int B;
  16.         public int C { get; set; }
  17.     }
  18.  
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             Foo foo = new Foo {A = 13, B = 37, C = 1488};
  24.             var fields = foo.GetType().GetFields(); // все поля класса Foo
  25.  
  26.             Dictionary<string, int> dict = new Dictionary<string, int>();
  27.  
  28.             foreach (var field in fields)
  29.             {
  30.                 var val = field.GetValue(foo);
  31.                 Console.WriteLine("{0} = {1}", field.Name, val);
  32.                 dict.Add(field.Name, (int) val);
  33.             }
  34.  
  35.             Console.WriteLine("-- *** --");
  36.             string message = "A:325469B:24348C:228420";
  37.  
  38.             // тупа строчку разбиваем
  39.  
  40.             // i++ <=> return i; i = i + 1;
  41.             // ++i <=> i = i + 1; return i;
  42.  
  43.             int i = 0;
  44.             while (i < message.Length)
  45.             {
  46.                 if (char.IsLetter(message[i]))
  47.                 {
  48.                     string currentField = message[i].ToString();
  49.  
  50.                     while (message[++i] != ':')
  51.                         currentField += message[i];
  52.  
  53.                     string currentValue = message[++i].ToString();
  54.  
  55.                     while (char.IsDigit(message[++i]))
  56.                     {
  57.                         currentValue += message[i];
  58.                         if (i + 1 == message.Length)
  59.                         {
  60.                             i++;
  61.                             break;
  62.                         }
  63.                     }
  64.  
  65.                     dict[currentField] = int.Parse(currentValue);
  66.                 }
  67.             }
  68.  
  69.             // тупа закончили строчку разбивать
  70.  
  71.             for ( i = 0; i < fields.Length; i++)
  72.             {
  73.                 foreach (var key in dict.Keys)
  74.                     if (fields[i].Name == key)
  75.                         fields[i].SetValue(foo, dict[key]);
  76.             }
  77.  
  78.             Console.WriteLine($"A = {foo.A}");
  79.             Console.WriteLine($"B = {foo.B}");
  80.             Console.WriteLine($"C = {foo.C} - свойство не определилось методом GetFields()");
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement