c# - How to do Activator.CreateInstance for various constructors? -


i need subclasses of datacontext , found below function lying around somewhere , sure finds subclass can't instantiate :(

  public static ienumerable<t> getsubclassesfor<t>(assembly assembly)   {    return (assembly.gettypes()     .where(t => t.basetype == (typeof (t)))     .select(t => (t) activator.createinstance(t, new object[] {"asdasd"})))     .tolist();   } 

i following error message:

system.reflection.targetinvocationexception : ett undantagsfel har inträffat målet för en aktivering. ----> system.typeinitializationexception : typinitieraren för playngo.cashier.data.cashierdc utlöste ett undantag. ----> system.nullreferenceexception : objektreferensen har inte angetts till en instans av ett objekt. vid system.runtimemethodhandle.invokeconstructor(iruntimemethodinfo method, object[] args, ref signaturestruct signature, runtimetype declaringtype) vid system.reflection.runtimeconstructorinfo.invoke(bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) vid system.runtimetype.createinstanceimpl(bindingflags bindingattr, binder binder, object[] args, cultureinfo culture, object[] activationattributes) vid system.activator.createinstance(type type, object[] args) vid playngo.dbml.reflexion.b_3(type t) reflexion.cs: line 23 vid system.linq.enumerable.whereselectarrayiterator2.movenext() vid system.collections.generic.list1..ctor(ienumerable1 collection) vid system.linq.enumerable.tolist(ienumerable1 source) vid playngo.dbml.reflexion.getinstances(assembly assembly) reflexion.cs: line 23 vid playngo.dbml.unittests.testreflection.testgettingtypes() testreflection.cs: line 21 --typeinitializationexception vid playngo.cashier.data.cashierdc..ctor(string connection) --nullreferenceexception vid playngo.cashier.data.cashierdc..cctor()

the constructor want call following:

    public cashierdc(string connection) :    base(connection, mappingsource) 

mappingsource directly instantiated , instance field. go wrong? have make work?

ps. .net 4.0

the targetinvocationexception indicates constructor invoking has thrown exception. perhaps due actual value passing in connection string. might want try using debugger , set break point in constructor taking connection string , (1) make sure being invoked , (2) determine exception occurring.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -