Source/NFX/Environment/FactoryUtils.cs
/// <summary>
/// Invokes a constructor for type feeding it the specified args:
///  node{type="NS.Type, Assembly" arg0=1 arg1=true....}
/// If the typePattern is passed, then the '*' in pattern is replaced with 'type' attr content.
/// This is needed for security, as this method allows to inject any type with any ctor params when typePattern is null
/// </summary>
Invokes a constructor for type feeding it the specified args:
node{type="NS.Type, Assembly" arg0=1 arg1=true....}
If the typePattern is passed, then the '*' in pattern is replaced with 'type' attr content.
This is needed for security, as this method allows to inject any type with any ctor params when typePattern is null
Invokes a constructor for type feeding it the specified args : node { type = " NS . Type Assembly " arg0 = 1 arg1 = true .... } If the typePattern is passed then the ' * ' in pattern is replaced with ' type ' attr content . This is needed for security as this method allows to inject any type with any ctor params when typePattern is null
public static T MakeUsingCtor<T>(IConfigSectionNode node, string typePattern = null)
        {
           string tpn = CoreConsts.UNKNOWN;
           try
           {
              if (node==null || !node.Exists)
                throw new ConfigException(StringConsts.ARGUMENT_ERROR+"FactoryUtils.Make(node==null|empty)");
              tpn = node.AttrByName(CONFIG_TYPE_ATTR).Value;
              if (tpn.IsNullOrWhiteSpace())
                tpn = typeof(T).AssemblyQualifiedName;
              else
                if (typePattern.IsNotNullOrWhiteSpace())
                  tpn = typePattern.Replace("*", tpn);
              var tp = Type.GetType(tpn, true);
              var args = new List<object>();
              for(var i=0; true; i++)
              {
                var attr = node.AttrByName("arg{0}".Args(i));
                if (!attr.Exists) break;
                args.Add(attr.Value);
              }
              var cinfo = tp.GetConstructors().FirstOrDefault(ci => ci.GetParameters().Length == args.Count);
              if (cinfo==null) throw new NFXException(".ctor arg count mismatch");
              for(var i=0; i<args.Count; i++)
                args[i] = args[i].ToString().AsType(cinfo.GetParameters()[i].ParameterType);
              try
              {
                return (T)Activator.CreateInstance(tp, args.ToArray());
              }
              catch(TargetInvocationException tie)
              {
                throw tie.InnerException;
              }
           }
           catch(Exception error)
           {
              throw new ConfigException(StringConsts.CONFIGURATION_MAKE_USING_CTOR_ERROR.Args(tpn, error.ToMessageWithType()), error);
           }
        }
public static T MakeUsingCtor < T > ( IConfigSectionNode node , string typePattern = null ) { string tpn = CoreConsts . UNKNOWN ; try { if ( node == null || ! node . Exists ) throw new ConfigException ( StringConsts . ARGUMENT_ERROR + " " ) ; tpn = node . AttrByName ( CONFIG_TYPE_ATTR ) . Value ; if ( tpn . IsNullOrWhiteSpace ( ) ) tpn = typeof ( T ) . AssemblyQualifiedName ; else if ( typePattern . IsNotNullOrWhiteSpace ( ) ) tpn = typePattern . Replace ( " " , tpn ) ; var tp = Type . GetType ( tpn , true ) ; var args = new List < object > ( ) ; for ( var i = 0 ; true ; i ++ ) { var attr = node . AttrByName ( " " . Args ( i ) ) ; if ( ! attr . Exists ) break ; args . Add ( attr . Value ) ; } var cinfo = tp . GetConstructors ( ) . FirstOrDefault ( ci => ci . GetParameters ( ) . Length == args . Count ) ; if ( cinfo == null ) throw new NFXException ( " " ) ; for ( var i = 0 ; i < args . Count ; i ++ ) args [ i ] = args [ i ] . ToString ( ) . AsType ( cinfo . GetParameters ( ) [ i ] . ParameterType ) ; try { return ( T ) Activator . CreateInstance ( tp , args . ToArray ( ) ) ; } catch ( TargetInvocationException tie ) { throw tie . InnerException ; } } catch ( Exception error ) { throw new ConfigException ( StringConsts . CONFIGURATION_MAKE_USING_CTOR_ERROR . Args ( tpn , error . ToMessageWithType ( ) ) , error ) ; } }
Invokes a constructor for type feeding it the specified args:
node{type="NS.Type, Assembly" arg0=1 arg1=true....}
If the typePattern is passed, then the '*' in pattern is replaced with 'type' attr content.
Invokes a constructor for type feeding it the specified args : node { type = " NS . Type Assembly " arg0 = 1 arg1 = true .... } If the typePattern is passed then the ' * ' in pattern is replaced with ' type ' attr content .
//dynamically re-cast argument types
node
IConfigSectionNode
typePattern
string