public void SetMember(MemberInfo member0)
 {
     lock (M2C)
     {
         M2C[member0] = this;
     }
     if (member0 is Type)
     {
         IsNonValue = true;
         return;
     }
     member = member0;
     if (MemberInfoExtensions.MemberIsReadOnly(member0))
     {
         // cant restore?
         // SkipSaveOnExit = true;
         IsReadOnly = true;
     }
     IsStatic     = MemberInfoExtensions.MemberIsStatic(member0);
     UseSingleton = !IsStatic;
     ReturnType   = MemberInfoExtensions.MemberReturnType(member0);
     if (ReturnType.IsGenericType && ReturnType.GetGenericTypeDefinition() == (typeof(ListAsSet <>)))
     {
         return;
     }
 }
 public static bool HasAttribute(this MemberInfo info, Type type)
 {
     if (NullCheck(type))
     {
         return(false);
     }
     return(info.IsDefined(type, true));
 }
        public static bool IsPossibleConfig(MemberInfo info)
        {
            if (MemberInfoExtensions.HasAttribute(info, typeof(ConfigSettingAttribute)))
            {
                return(true);
            }
            if (MemberInfoExtensions.HasAttribute(info, typeof(NotConfigurable)))
            {
                return(false);
            }

            if (info.DeclaringType.IsEnum)
            {
                return(false);
            }
            {
                var inf = info as FieldInfo;
                if (inf != null && (inf.IsStatic || ConfigSettingAttribute.IsSingletonClass(inf.DeclaringType)) &&
                    !inf.IsInitOnly && !inf.IsLiteral)
                {
                    if (MemberInfoExtensions.IsSettableType(inf.FieldType))
                    {
                        if (inf.DeclaringType.IsValueType)
                        {
                            return(false);
                        }
                        return(true);
                    }
                }
            }
            {
                var inf = info as PropertyInfo;
                if (inf != null)
                {
                    if (inf.GetSetMethod() == null)
                    {
                        return(false);
                    }
                    info = inf.GetGetMethod();
                    var inf0 = info as MethodInfo;
                    if (inf0 != null && (inf0.IsStatic || ConfigSettingAttribute.IsSingletonClass(inf0.DeclaringType)) &&
                        inf.CanRead && inf.CanWrite)
                    {
                        if (MemberInfoExtensions.IsSettableType(inf.PropertyType))
                        {
                            if (inf0.DeclaringType.IsValueType)
                            {
                                return(false);
                            }
                            return(true);
                        }
                        ;
                    }
                }
            }
            return(false);
        }
        public static bool MemberIsStatic(this MemberInfo info)
        {
            if (NullCheck(info))
            {
                return(false);
            }

            if (info.DeclaringType.IsEnum)
            {
                return(true);
            }
            {
                var inf = info as _Type;
                if (inf != null)
                {
                    if (inf.IsValueType)
                    {
                        return(true);
                    }
                }
            }
            {
                var inf = info as _EventInfo;
                if (inf != null)
                {
                    info = inf.GetRaiseMethod(true);
                }
            }
            {
                var inf = info as _FieldInfo;
                if (inf != null)
                {
                    return(inf.IsStatic);
                }
            }
            {
                var inf = info as _PropertyInfo;
                if (inf != null)
                {
                    info = inf.GetGetMethod(true) ?? inf.GetSetMethod(true);
                }
            }
            {
                var inf = info as _MethodInfo;
                if (inf != null)
                {
                    return(inf.IsStatic);
                }
            }
            DLRConsole.DebugWriteLine("cant detect isStatic on " + info);
            return(false);
        }
        public static bool LikelySingletonValueMember(MemberInfo fnd0)
        {
            if (CantBeSingletonValueMember(fnd0))
            {
                return(false);
            }
            string lower = fnd0.Name.ToLower();

            if (
                !(lower.Contains("global") || lower.Contains("shared") || lower.Contains("single") ||
                  lower.Contains("static")))
            {
                return(false);
            }
            return(false);
        }
        private static bool SkipOnExit(MemberInfo s)
        {
            ConfigSettingAttribute cs0 = FindConfigSetting(s, false);

            if (cs0.VSkipSaveOnExit.HasValue)
            {
                return(cs0.VSkipSaveOnExit.Value);
            }
            ConfigSettingAttribute cs1 = FindConfigSetting(s.DeclaringType, false);

            if (cs1 != null && cs1.VSkipSaveOnExit.HasValue)
            {
                return(cs1.VSkipSaveOnExit.Value);
            }
            return(false);
        }
 static public bool MemberIsReadOnly(this MemberInfo member)
 {
     if (NullCheck(member))
     {
         return(false);
     }
     if (member != null)
     {
         var fi = member as FieldInfo;
         if (fi != null)
         {
             return(fi.IsLiteral || fi.IsInitOnly);
         }
         var pi = member as PropertyInfo;
         if (pi != null)
         {
             return(!pi.CanWrite);
         }
     }
     return(false);
 }
 public static Type MemberReturnType(this MemberInfo info)
 {
     if (NullCheck(info))
     {
         return(null);
     }
     if (info.DeclaringType.IsEnum)
     {
         return(info.DeclaringType);
     }
     {
         var inf = info as _FieldInfo;
         if (inf != null)
         {
             return(inf.FieldType);
         }
     }
     {
         var inf = info as _PropertyInfo;
         if (inf != null)
         {
             return(inf.PropertyType);
         }
     }
     {
         var inf = info as _MethodInfo;
         if (inf != null)
         {
             return(inf.ReturnType);
         }
     }
     {
         var inf = info as ConstructorInfo;
         if (inf != null)
         {
             return(inf.ReflectedType);
         }
     }
     return(info.DeclaringType);
 }
 public static object MemberValue(this MemberInfo m, object target)
 {
     if (NullCheck(m))
     {
         return(target);
     }
     if (m is FieldInfo)
     {
         var inf = m as FieldInfo;
         return(inf.GetValue(target));
     }
     if (m is PropertyInfo)
     {
         var inf = m as PropertyInfo;
         m = inf.GetGetMethod();
     }
     if (m is MethodInfo)
     {
         var inf = m as MethodInfo;
         return(inf.Invoke(target, null));
     }
     return(null);
 }
        public static ConfigSettingAttribute FindConfigSetting(MemberInfo s, bool forceCreate)
        {
            const bool             createIfDeclared = true;
            ConfigSettingAttribute cs0;

            lock (M2C)
            {
                if (!M2C.TryGetValue(s, out cs0))
                {
                    if (createIfDeclared)
                    {
                        if (s.IsDefined(typeof(ConfigSettingAttribute), true))
                        {
                            var cs = s.GetCustomAttributes(typeof(ConfigSettingAttribute), true);
                            if (cs != null && cs.Length > 0)
                            {
                                cs0 = (ConfigSettingAttribute)cs[0];
                                cs0.SetMember(s);
                            }
                            else
                            {
                                cs0 = null;
                            }
                        }
                        M2C[s] = cs0;
                    }
                }
                if (cs0 == null && forceCreate)
                {
                    cs0 = new ConfigSettingAttribute();
                    cs0.SetMember(s);
                    M2C[s] = cs0;
                }
            }
            return(cs0);
        }
 public static bool CantBeSingletonValueMember(MemberInfo fnd0)
 {
     if (fnd0 == null)
     {
         return(true);
     }
     if (fnd0 is _EventInfo)
     {
         return(true);
     }
     if (fnd0 is Type)
     {
         return(true);
     }
     if (!MemberInfoExtensions.MemberIsStatic(fnd0))
     {
         return(true);
     }
     if (fnd0.MemberReturnType() == null)
     {
         return(true);
     }
     return(false);
 }
 public static bool HasAttribute(MemberInfo s, Type type)
 {
     return(s.HasAttribute(type));
 }
        public static bool IsGoodForConfig(MemberInfo info, bool notDeclaredOK, bool notPublicOK, bool notWriteableOK, bool notFromStringOK)
        {
            if (MemberInfoExtensions.HasAttribute(info, typeof(ConfigSettingAttribute)))
            {
                return(true);
            }
            if (MemberInfoExtensions.HasAttribute(info, typeof(NotConfigurable)))
            {
                return(false);
            }
            if (!notDeclaredOK)
            {
                return(false);
            }

            if (info.DeclaringType.IsEnum)
            {
                return(false);
            }
            {
                var inf = info as FieldInfo;
                if (inf != null)
                {
                    bool readOnly = inf.IsInitOnly || inf.IsLiteral;
                    if ((inf.IsStatic || IsSingletonClass(inf.DeclaringType)) && (notPublicOK || inf.IsPublic) &&
                        (notWriteableOK || !readOnly))
                    {
                        if (!notFromStringOK)
                        {
                            if (inf.FieldType.IsArray)
                            {
                                return(false);
                            }
                        }
                        //if (readOnly) notFromStringOK = true;
                        if (notFromStringOK || MemberInfoExtensions.IsSettableType(inf.FieldType))
                        {
                            if (inf.DeclaringType.IsValueType)
                            {
                                if (readOnly)
                                {
                                    return(false);
                                }
                                return(false);
                            }
                            return(true);
                        }
                    }
                }
            }
            {
                var inf = info as PropertyInfo;
                if (inf != null)
                {
                    MethodInfo m = inf.GetGetMethod(true) ?? inf.GetSetMethod(true);
                    if ((m.IsStatic || IsSingletonClass(inf.DeclaringType)) &&
                        (notPublicOK || m.IsPublic) && inf.CanRead && (notWriteableOK || inf.CanWrite))
                    {
                        bool readOnly = !inf.CanWrite;
                        //if (readOnly) notFromStringOK = true;
                        if (notFromStringOK || MemberInfoExtensions.IsSettableType(inf.PropertyType))
                        {
                            if (inf.DeclaringType.IsValueType)
                            {
                                if (readOnly)
                                {
                                    return(false);
                                }
                                return(false);
                            }
                            return(true);
                        }
                        ;
                    }
                }
            }
            return(false);
        }
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <filterpriority>2</filterpriority>
 public void Dispose()
 {
     member       = null;
     _singleton   = null;
     initialValue = null;
 }