Source/NFX/Environment/Attributes.cs
/// <summary>
/// Applies config values to fields/properties as specified by config attributes
/// </summary>
Applies config values to fields/properties as specified by config attributes
Applies config values to fields / properties as specified by config attributes
public static object Apply(object entity, IConfigSectionNode node)
       {
         if (entity==null || node==null) return entity;
         var etp = entity.GetType();
         var macroAttr = etp.GetCustomAttributes(typeof(ConfigMacroContextAttribute), true).FirstOrDefault() as ConfigMacroContextAttribute;
         if (macroAttr!=null)
             node.Configuration.MacroRunnerContext = entity;
         var cattr = etp.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute;
         if (cattr!=null)
         {
           cattr.evalAttributeVars(etp);
           var path = cattr.Path ?? StringConsts.NULL_STRING;
           node = node.Navigate(path) as ConfigSectionNode;
           if (node==null)
            throw new ConfigException(string.Format(StringConsts.CONFIGURATION_NAVIGATION_REQUIRED_ERROR, cattr.Path));
         }
         var members =  getAllFieldsOrProps( etp );
         foreach(var mem in members)
         {
           var mattr = mem.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault() as ConfigAttribute;
           if (mattr==null) continue;
           if (string.IsNullOrWhiteSpace(mattr.Path))
                mattr.Path =  GetConfigPathsForMember(mem);
           mattr.evalAttributeVars(etp);
           var mnode = node.Navigate(mattr.Path);
           if (mem.MemberType == MemberTypes.Field)
           {
                 var finf = (FieldInfo)mem;
                 if (typeof(IConfigSectionNode).IsAssignableFrom(finf.FieldType))
                 {
                       if (finf.IsInitOnly)
                         throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name));
                       var snode = mnode as IConfigSectionNode;
                       if (snode==null)
                          throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, finf.Name));
                       finf.SetValue(entity, mnode);
                 }
                 else
                 if (typeof(IConfigurable).IsAssignableFrom(finf.FieldType))
                 {
                       var snode = mnode as IConfigSectionNode;
                       if (snode==null)
                          throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, finf.Name));
                       if (finf.GetValue(entity)!=null)
                         ((IConfigurable)finf.GetValue(entity)).Configure(snode);
                 }
                 else
                 {
                       if (finf.IsInitOnly)
                         throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, finf.Name));
                       if (mnode.Exists && mnode.VerbatimValue!=null)
                        finf.SetValue(entity, getVal(mnode, finf.FieldType, etp.FullName, finf.Name, mattr.Verbatim));
                       else
                        if (mattr.Default!=null) finf.SetValue(entity, mattr.Default);
                 }
           }
           else
           if (mem.MemberType == MemberTypes.Property)
           {
                 var pinf = (PropertyInfo)mem;
                 if (typeof(IConfigSectionNode).IsAssignableFrom(pinf.PropertyType))
                 {
                       if (!pinf.CanWrite)
                         throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name));
                       var snode = mnode as IConfigSectionNode;
                       if (snode==null)
                          throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR, etp.FullName, pinf.Name));
                       pinf.SetValue(entity, mnode, null);
                 }
                 else
                 if (typeof(IConfigurable).IsAssignableFrom(pinf.PropertyType))
                 {
                       var snode = mnode as IConfigSectionNode;
                       if (snode==null)
                          throw new ConfigException(string.Format(StringConsts.CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR, etp.FullName, pinf.Name));
                       if (pinf.GetValue(entity, null)!=null)
                         ((IConfigurable)pinf.GetValue(entity, null)).Configure(snode);
                 }
                 else
                 {
                       if (!pinf.CanWrite)
                         throw new ConfigException(string.Format(StringConsts.CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR, etp.FullName, pinf.Name));
                       if (mnode.Exists && mnode.VerbatimValue!=null)
                        pinf.SetValue(entity, getVal(mnode,  pinf.PropertyType, etp.FullName, pinf.Name, mattr.Verbatim), null);
                       else
                        if (mattr.Default!=null) pinf.SetValue(entity, mattr.Default, null);
                 }
           }
         }
        return entity;
       }
public static object Apply ( object entity , IConfigSectionNode node ) { if ( entity == null || node == null ) return entity ; var etp = entity . GetType ( ) ; var macroAttr = etp . GetCustomAttributes ( typeof ( ConfigMacroContextAttribute ) , true ) . FirstOrDefault ( ) as ConfigMacroContextAttribute ; if ( macroAttr != null ) node . Configuration . MacroRunnerContext = entity ; var cattr = etp . GetCustomAttributes ( typeof ( ConfigAttribute ) , true ) . FirstOrDefault ( ) as ConfigAttribute ; if ( cattr != null ) { cattr . evalAttributeVars ( etp ) ; var path = cattr . Path ?? StringConsts . NULL_STRING ; node = node . Navigate ( path ) as ConfigSectionNode ; if ( node == null ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_NAVIGATION_REQUIRED_ERROR , cattr . Path ) ) ; } var members = getAllFieldsOrProps ( etp ) ; foreach ( var mem in members ) { var mattr = mem . GetCustomAttributes ( typeof ( ConfigAttribute ) , true ) . FirstOrDefault ( ) as ConfigAttribute ; if ( mattr == null ) continue ; if ( string . IsNullOrWhiteSpace ( mattr . Path ) ) mattr . Path = GetConfigPathsForMember ( mem ) ; mattr . evalAttributeVars ( etp ) ; var mnode = node . Navigate ( mattr . Path ) ; if ( mem . MemberType == MemberTypes . Field ) { var finf = ( FieldInfo ) mem ; if ( typeof ( IConfigSectionNode ) . IsAssignableFrom ( finf . FieldType ) ) { if ( finf . IsInitOnly ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR , etp . FullName , finf . Name ) ) ; var snode = mnode as IConfigSectionNode ; if ( snode == null ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR , etp . FullName , finf . Name ) ) ; finf . SetValue ( entity , mnode ) ; } else if ( typeof ( IConfigurable ) . IsAssignableFrom ( finf . FieldType ) ) { var snode = mnode as IConfigSectionNode ; if ( snode == null ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR , etp . FullName , finf . Name ) ) ; if ( finf . GetValue ( entity ) != null ) ( ( IConfigurable ) finf . GetValue ( entity ) ) . Configure ( snode ) ; } else { if ( finf . IsInitOnly ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR , etp . FullName , finf . Name ) ) ; if ( mnode . Exists && mnode . VerbatimValue != null ) finf . SetValue ( entity , getVal ( mnode , finf . FieldType , etp . FullName , finf . Name , mattr . Verbatim ) ) ; else if ( mattr . Default != null ) finf . SetValue ( entity , mattr . Default ) ; } } else if ( mem . MemberType == MemberTypes . Property ) { var pinf = ( PropertyInfo ) mem ; if ( typeof ( IConfigSectionNode ) . IsAssignableFrom ( pinf . PropertyType ) ) { if ( ! pinf . CanWrite ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR , etp . FullName , pinf . Name ) ) ; var snode = mnode as IConfigSectionNode ; if ( snode == null ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_PATH_ICONFIGSECTION_SECTION_ERROR , etp . FullName , pinf . Name ) ) ; pinf . SetValue ( entity , mnode , null ) ; } else if ( typeof ( IConfigurable ) . IsAssignableFrom ( pinf . PropertyType ) ) { var snode = mnode as IConfigSectionNode ; if ( snode == null ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_PATH_ICONFIGURABLE_SECTION_ERROR , etp . FullName , pinf . Name ) ) ; if ( pinf . GetValue ( entity , null ) != null ) ( ( IConfigurable ) pinf . GetValue ( entity , null ) ) . Configure ( snode ) ; } else { if ( ! pinf . CanWrite ) throw new ConfigException ( string . Format ( StringConsts . CONFIGURATION_ATTRIBUTE_MEMBER_READONLY_ERROR , etp . FullName , pinf . Name ) ) ; if ( mnode . Exists && mnode . VerbatimValue != null ) pinf . SetValue ( entity , getVal ( mnode , pinf . PropertyType , etp . FullName , pinf . Name , mattr . Verbatim ) , null ) ; else if ( mattr . Default != null ) pinf . SetValue ( entity , mattr . Default , null ) ; } } } return entity ; }
Applies config values to fields/properties as specified by config attributes
Applies config values to fields / properties as specified by config attributes
//20130708 DKh - support for [ConfigMacroContext] injection //--- //rebase root config node per supplied path //20130708 DKh - default attribute name taken from member name if path==null //--- //for members
entity
object
node
IConfigSectionNode