Activator.CreateInstance always returns null for NameValueListBase class

Activator.CreateInstance always returns null for NameValueListBase class

Old forum URL: forums.lhotka.net/forums/t/10595.aspx


TSF posted on Friday, August 05, 2011

Using CSLA 4.1 / Encapsulated Invocation.  We have other non-NVLB objects that are working fine.  However, we did our first NVLB object and when the dalManager.GetProvider method hits this line:

return Activator.CreateInstance(type) as T;

 It always returns null.  However, the variable type looks correct at runtime.   {Company.BusinessObject.Transportation.DataAccess.Sql.ShipMethodListDal}

Any thoughts on what we should check?  Thanks.
Tim

tmg4340 replied on Saturday, August 06, 2011

Without more information, the only answer I can provide is the obvious one: the "as" operator will return a NULL when the object cannot be converted into the type requested.  So the only thing I can presume is that the type you're passing into CreateInstance cannot be converted into whatever "T" is.

(Unfortunately, based on your description, I'm not sure if "type" is "ShipMethodListDal" or if "T" is...)

HTH

- Scott

TSF replied on Monday, August 08, 2011

Thanks to both for your responses.  I tried Jonny's suggestion, but when that didn't work I went back to pondering Scott's "whatever 'T' is" comment.  I began to dig further and found that the ShipMethodListDal class was not implementing IShipMethodListDal.  As soon as we added that it started working.

class ShipMethodListDal : ISmartRegionInfoListDal {

     public System.Data.IDataReader Fetch() { }

}

 

JonnyBee replied on Sunday, August 07, 2011

I have seen the same type of problem with generic objects that have more than one generic constraints.

IIRC changing to use Csla.Reflection.MethodCaller.CreateInstance(type) did work as expected.

Which runtime are you using (NET/SL/WP)?

Copyright (c) Marimer LLC