Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- namespace TestArrays
- {
- internal class Program
- {
- private static void Main()
- {
- var lsc = new List<SomeClass>();
- for(Int32 i = 0; i < 10; i++)
- lsc.Add(new SomeClass());
- lsc.ForEach(x => Console.WriteLine(x.GetSomeValue("StringField")));
- }
- }
- internal class SomeClass
- {
- public readonly Int32 IntField;
- public readonly String StringField;
- public readonly Double DoubleField;
- private static readonly Random Rand = new Random();
- public SomeClass()
- {
- this.IntField = Rand.Next(0, 50);
- this.DoubleField = Rand.NextDouble();
- var array = new Byte[this.IntField];
- Rand.NextBytes(array);
- this.StringField = Encoding.UTF8.GetString(array);
- }
- }
- internal static class SomeClassExtentions
- {
- public static String GetSomeValue(this SomeClass classInstance, String fieldName)
- {
- const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
- FieldInfo fi = typeof(SomeClass).GetField(fieldName, flags);
- return fi != null ? fi.GetValue(classInstance).ToString() : null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement