Activator.CreateInstance error

I

ismail.mayat

Hello,

I am having a problem with Activator.CreateInstance. I have the
following code

Assembly asm = System.Reflection.Assembly.LoadFrom(assembly);
if(asm!=null)
{
try
{
foreach (Type t in asm.GetTypes())
{
Type hasInterface =
t.GetInterface("umbracoUtilities.Businesslogic.IUmbracoSearchFileFilter",
true);
//writeOutInterfaces(hasInterface);
if (hasInterface != null && !t.IsInterface)
{
//fails here


umbracoUtilities.Businesslogic.IUmbracoSearchFileFilter typeInstance =
Activator.CreateInstance(t) as
umbracoUtilities.Businesslogic.IUmbracoSearchFileFilter;

string extensions = ",";
if(typeInstance!=null)
{
foreach(string ext in typeInstance.extensions)
{
extensions += ext + ",";
_filters.Add(extensions,typeInstance);
System.Web.HttpContext.Current.Trace.Write("umbracoUtilities.Search.FileFilterFactory",
" + Adding searchfilter for extensions '" + extensions + "'");
}
}
else{
System.Web.HttpContext.Current.Trace.Warn("umbracoUtilities.Search.FileFilterFactory",
" + Could not get instance for " +t.ToString() + " in '" + assembly +
"'");
}
}
}
}
catch (Exception factoryE)
{
System.Web.HttpContext.Current.Trace.Warn("umbracoUtilities.Search.FileFilterFactory",
"error while processing assembly " + assembly, factoryE);
}
}

It all works upto to the comment //fails here . The assembly is loaded
fine the if checks all work and when it gets to
Activator.CreateInstance the type it is trying to create definately
implements the interface specified. Also the type it is trying to
create has a parameterless constructor. I do not get any errors anyone
any suggestions ?

Many thanks in advance

Ismail
 
T

Tasos Vogiatzoglou

What is the exception ?

Is the default constructor visible ?

Regards,
Tasos
 
I

Ismail

Tasos,

There is default constructor for types implementing the interface. I
get null back so for line

umbracoUtilities.Businesslogic.IUmbracoSearchFileFilter typeInstance =
Activator.CreateInstance(t) as
umbracoUtilities.Businesslogic.IUmbracoSearchFileFilter;

I get typeInstance set to null but further on i do
if(typeInstance!=null) so I am trapping any potential null exception
that would arise when i use typeInstance. I have also tried
CreateInstancefrom method but I still get the same problem.

Regards

Ismail
 
I

Ismail

Tasos,

Null reference exception the createinstance does not create and we get
null.

However when i change the code so that instead of loadfrom i do
getexecutingasesmbly

//Assembly asm = System.Reflection.Assembly.LoadFrom(assembly);
Assembly asm =System.Reflection.Assembly.GetExecutingAssembly();

assembly it works. The interface and filters are in current assembly
i used to have it in different one but tried to get it to work by
putting in same assembly. Not sure why when you do loadfrom
createinstance does not work but does work when using
getexecutingassembly.

Regards

Ismial
 
T

Tasos Vogiatzoglou

Ismail,

The assembly loaded with LoadFrom, is loaded or not ?

Can you assert that Assembly asm = Assembly.LoadFrom(path); actually
returns an assembly?

if this is not the case, try to see if there are any unresolved
dependencies that prohibit the assembly from loading.

It loads with getExecutingAssembly because the assembly is already
loaded (I presume)

Regards,
Tasos

(It would be a good thing to provide a stactkrace)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top