Advertisement
N1K003

Untitled

Jun 30th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5.  
  6. namespace TestArrays
  7. {
  8.     internal class Program
  9.     {
  10.         private static void Main()
  11.         {
  12.             var lsc = new List<SomeClass>();
  13.             for(Int32 i = 0; i < 10; i++)
  14.                 lsc.Add(new SomeClass());
  15.  
  16.             lsc.ForEach(x => Console.WriteLine(x.GetSomeValue("StringField")));
  17.         }
  18.     }
  19.  
  20.     internal class SomeClass
  21.     {
  22.         public readonly Int32 IntField;
  23.         public readonly String StringField;
  24.         public readonly Double DoubleField;
  25.  
  26.         private static readonly Random Rand = new Random();
  27.  
  28.         public SomeClass()
  29.         {
  30.            
  31.             this.IntField = Rand.Next(0, 50);
  32.             this.DoubleField = Rand.NextDouble();
  33.  
  34.             var array = new Byte[this.IntField];
  35.             Rand.NextBytes(array);
  36.             this.StringField = Encoding.UTF8.GetString(array);
  37.         }
  38.     }
  39.  
  40.     internal static class SomeClassExtentions
  41.     {
  42.         public static String GetSomeValue(this SomeClass classInstance, String fieldName)
  43.         {
  44.             const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
  45.  
  46.             FieldInfo fi = typeof(SomeClass).GetField(fieldName, flags);
  47.             return fi != null ? fi.GetValue(classInstance).ToString() : null;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement