c# - Module properties from xml, providing default values -


if have following module:

class mymodule : module {     public double somedouble { get; set; } } 

and have following autofac section in app.config

<autofac>    <modules>       <module type="mynamespace.mymodule, mynamespace">          <properties>             <property name="somedouble" value="" />          </properties>       </module>    </modules> </autofac> 

when calling builer.build()

i exception saying:

input string not int correct format, value not valid double

which makes sense.

what able provide kind of handler handle exception instead of making whole build() fail, or provide default value.

is possible within autofac @ moment?

there no easy way this. solution can see provide custom iconfigurationregistrar configurationsettingsreader

var builder = new containerbuilder(); builder.registermodule(new configurationsettingsreader() {     configurationregistrar = new customconfigurationregistrar() });  builder.build(); 

the customconfigurationregistrar can inherits default configurationregistrar. if @ source code, can see registerconfiguredmodules register module in foreach loop, there no method register single module, have copy/past base class.

because error come parameter used inject property, can create lazyparameter handle error , return default value.

public class customconfigurationregistrar : configurationregistrar {     internal class lazyparameter : parameter     {         public lazyparameter(parameter originalparameter)         {             this._originalparameter = originalparameter;         }          private readonly parameter _originalparameter;          public override boolean cansupplyvalue(parameterinfo pi, icomponentcontext context, out func<object> valueprovider)         {             boolean cansupplyvalue = this._originalparameter.cansupplyvalue(pi, context, out valueprovider);             if (cansupplyvalue)             {                 func<object> originalvalueprovider = valueprovider;                 valueprovider = () =>                 {                     try                     {                         return originalvalueprovider();                     }                     catch                     {                         console.writeline(pi.member.name);                         // log , throw or return default value                         return null;                     }                 };             }             return cansupplyvalue;         }     }       protected override void registerconfiguredmodules(containerbuilder builder, sectionhandler configurationsection)     {         if (builder == null)         {             throw new argumentnullexception("builder");         }         if (configurationsection == null)         {             throw new argumentnullexception("configurationsection");         }         foreach (moduleelement moduleelement in configurationsection.modules)         {             this.registerconfiguredmodule(builder, configurationsection, moduleelement);         }     }      private void registerconfiguredmodule(containerbuilder builder, sectionhandler configurationsection, moduleelement moduleelement)     {         try         {             var moduletype = this.loadtype(moduleelement.type, configurationsection.defaultassembly);             imodule module = null;              ienumerable<parameter> parameters = moduleelement.parameters.toparameters().select(p => new lazyparameter(p));             ienumerable<parameter> properties = moduleelement.properties.toparameters().select(p => new lazyparameter(p));              using (var moduleactivator = new reflectionactivator(                 moduletype,                 new defaultconstructorfinder(),                 new mostparametersconstructorselector(),                 parameters,                 properties))             {                 module = (imodule)moduleactivator.activateinstance(new containerbuilder().build(), enumerable.empty<parameter>());             }             builder.registermodule(module);         }         catch (exception)         {             // log             throw;         }     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -