C# Reflection: Create object from assembly without invoking constructorinfo -


i having problem calling constructor reflection. parameterless constructor no problem when i'm trying call once has parameter missingmethodexception.

code:

 if (type != null)         {             var constructor = type.getconstructor(type.emptytypes);             if (constructor != null)                return activator.createinstance(type);              constructor = type.getconstructors()[0];              var parameters = constructor.getparameters();              var obj = new object[parameters.length];              (var = 0; < parameters.length; i++)             {                 obj[i] = (object) parameters[i].parametertype;             }             return activator.createinstance(type, bindingflags.nonpublic | bindingflags.public | bindingflags.instance, null, obj, null);         } 

the parameterless constructor works fine.

 var constructor = type.getconstructor(type.emptytypes);             if (constructor != null)             {                 return activator.createinstance(type);             } 

this part not:

   constructor = type.getconstructors()[0];              var parameters = constructor.getparameters();              var obj = new object[parameters.length];              (var = 0; < parameters.length; i++)             {                 obj[i] = (object) parameters[i].parametertype;             }             return activator.createinstance(type, bindingflags.nonpublic | bindingflags.public | bindingflags.instance, null, obj, null);         } 

i know 1 of constructors looks this:

 public class yyy: xxx {     public yyy(guid customerid)         : base(404, level.warn, null, string.format("{0}",customerid))     {     } } 

i know parametertype not datatype constructor wants:

parameters[i].parametertype guid false 

and.. know if remove obj , put new guid() work:

return activator.createinstance(type, bindingflags.nonpublic | bindingflags.public | bindingflags.instance, null, new guid(), null); 

question: how can call constructor?

found clues from:

how identify each parameter type in c# method?

where @jaredpar saying following: actual type of parameter use parametertype on parameterinfo value. value there several ways can use identify type. easiest direct comparison known type.

so in case need identify type of parametertype , create new instance of type before send constructor.

because care of type of object, resolve by

var fixture = new fixture(); //ploeh.autofixture var obj = new specimencontext(fixture).resolve(asmtype); //ploeh.autofixture.kernel.specimencontext  

so after getting assemblytype create fixture , specimentcontext fixture , resolve asmtype.

this object of corresponding type.

so final result:

   foreach (var asmtype in assemblytypes)         {             var fixture = new fixture();             var obj = new specimencontext(fixture).resolve(asmtype);              _handler.error(new login { brandid = _brandid }, new domain.entities.customer { customerid = it.isany<guid>() }, (exception) obj);              if (obj invalidcredentialsexception)                 _loginrepository.verify(v => v.zzz(it.isany<guid>(), it.isany<int>()), times.once);             else                 _loginrepository.verify(v => v.zzz(it.isany<guid>(), it.isany<int>()), times.never);         } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -