Пример #1
0
        public static FieldAndObjectInfo GetStaticFieldInfo(string property)
        {
            var props = property.Split('.');
            if (props.Length > 1)
            {
                string className = props.First();
                var types = Assembly.GetExecutingAssembly().GetTypes();
                Type classType = null;
                foreach (var type in types)
                {
                    if (type.Name == className)
                    {
                        classType = type;
                        break;
                    }
                }
                if (classType != null)
                {
                    var field = classType.GetMember(props[1])[0];
                    if (props.Length > 2)
                    {
                        string prop = "";
                        for (int i = 2; i < props.Length; i++)
                        {
                            prop += props[i] + ((i < props.Length - 1) ? "." : "");
                        }
                        return GetFieldInfo(prop, field.GetValue(null));
                    }
                    else
                    {
                        var info = new FieldAndObjectInfo();
                        //info.Objects.Add(field.GetValue(null));
                        info.Fields.Add(field);
                        return info;
                    }

                }
            }
            return null;
        }
Пример #2
0
 public static FieldAndObjectInfo GetFieldInfo(string property, object o)
 {
     var props = property.Split('.');
     string finalProp = props[props.Length - 1];
     FieldInfo pi = null;
     object lastObj = o;
     FieldAndObjectInfo fao = new FieldAndObjectInfo() {  };
     foreach (var prop in props)
     {
         try
         {
             var pr = lastObj.GetType().GetMember(prop);
             if (prop == finalProp)
             {
                 fao.Fields.Add(pr[0]);
                 fao.Objects.Add(lastObj);
                 return fao;
             }
             fao.Fields.Add(pr[0]);
             fao.Objects.Add(lastObj);
             lastObj = pr[0].GetValue(lastObj);
         }
         catch
         {
             return null;
         }
     }
     return fao;
 }