Find Static Field Value In Assembly - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Find Static Field Value In Assembly

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//from   ww w  .ja  va2  s . co m

public class Main{
        public static IList<T> FindStaticFieldValueInAssembly<T>(this Assembly assembly, BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public) where T : class
        {
            IList<T> datas = new List<T>();

            var fieldList = assembly.FindFieldInAssembly<T>(bindingAttr);
            if (fieldList.Count > 0)
            {
                fieldList.ForEach(i =>
                {
                    var data = i.GetValue(null) as T;
                    if (data != null)
                    {
                        datas.Add(data);
                    }
                });
            }

            return datas;
        }
}

Related Tutorials