dynamically load DLL exception when calling Activator.CreateInstance(type, object[])

  • Thread starter Thread starter Nancy Sui
  • Start date Start date
N

Nancy Sui

I am trying to dynamically load a dll which specifies different rules
for evalution with the following code.
DataRow dr = ds.Tables[0].Rows[0];
string fileName = Convert.ToString(dr["RuleLocation"]);
assemblyInstance = Assembly.Load( fileName );
typeInstance = assemblyInstance.GetType( "MSF.Storefront.Rules." +
dr["RuleName"].ToString() , true, false);
Object[] parameter = new Object[1];
if (seg == null)
parameter[0] = cs;
else
parameter[0] = seg;

rule = (Rules.IBaseRule)Activator.CreateInstance(typeInstance,
parameter);
return rule;

this code works fine if the rules are existing .cs files in the
current project and it loads it fine. The problem is when the program
is already running and then I add a new rule .dll into the bin folder,
then try to load that rule dynamically. I get an exception.

the exception i get is the MissingMethodException, member cannot be
found. So then i create an empty constructor and pass in only the
typeInstance, i get a CastException. What is wrong? Please help.
Thanks.
 
Nancy:

If I am reading the description of the problem correctly, then it
sounds as if you are trying to create an instance of a type that is in
an assembly that is not loaded. Are you using Assembly.LoadFile
somewhere to bring that assembly into the process?
 
Back
Top