constructor - C#/MEF don't work with [ImportingConstrutor] with default values -


i have more or less same problem in question: c#/mef doesn't work base class without parameterless constructor

i understand answer well. mef doesn't know value val not able create instance.

but have no base ctor call , default value val:

[export(typeof(iprimitivedecomposer))] public class prim : iprimitivedecomposer {     [importingconstructor]      public prim(int val=0)     {//some code     }      public bool match(node node) {return true;} } 

the code compiles fine, mef doesn't seem have export iprimitivedecomposer when ask it. when following works fine:

[export(typeof(iprimitivedecomposer))] public class prim : iprimitivedecomposer {     public prim() : this(0)     public prim(int val=0)     {//some code     }      public bool match(node node) {return true;} } 

thx soko

optional arguments in c# compile time "trick". constructor generated first code not have parameterless constructor. instead, has constructor takes 1 integer, , decorated attributes provide default value. it's writing:

public prim([optional, defaultparametervalue(0)] int val) {    

the c# compiler knows these attributes, , "fill in" value at compile time when finds method or constructor requiring information.

mef not these attributes. requires default constructor, or constructor each of arguments provided composed types. first version fails in case, mef can't construct type.

using 2 constructors, showed, correct way handle mef.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -