Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace ReflectionGetConst
- {
- class Program
- {
- static void Main(string[] args)
- {
- var list = GetConstants< ClassTarget>();
- foreach (var a in list)
- {
- Console.WriteLine("{0} {1} {2} {3}", a.Attributes.ToString(), a.FieldType.FullName, a.Name, a.GetValue(null).ToString());
- }
- Console.ReadLine();
- }
- public static List<FieldInfo> GetConstants<T>()
- {
- var type = typeof (T);
- FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public| BindingFlags.NonPublic| BindingFlags.Static | BindingFlags.FlattenHierarchy);
- return fieldInfos.Where(fi => fi.IsLiteral).ToList();
- }
- }
- public class ClassTarget
- {
- public const int a = 0;
- public const int b = -10;
- public const int c = 20;
- public const int d = 3;
- private const int e = 10;
- private readonly int f = 4;
- private int g = 12;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement