473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Activator.CreateInstance error

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.IUm bracoSearchFileFilter",
true);
//writeOutInterfaces(hasInterface);
if (hasInterface != null && !t.IsInterface)
{
//fails here
umbracoUtilities.Businesslogic.IUmbracoSearchFileF ilter typeInstance =
Activator.CreateInstance(t) as
umbracoUtilities.Businesslogic.IUmbracoSearchFileF ilter;

string extensions = ",";
if(typeInstance!=null)
{
foreach(string ext in typeInstance.extensions)
{
extensions += ext + ",";
_filters.Add(extensions,typeInstance);
System.Web.HttpContext.Current.Trace.Write("umbrac oUtilities.Search.FileFilterFactory",
" + Adding searchfilter for extensions '" + extensions + "'");
}
}
else{
System.Web.HttpContext.Current.Trace.Warn("umbraco Utilities.Search.FileFilterFactory",
" + Could not get instance for " +t.ToString() + " in '" + assembly +
"'");
}
}
}
}
catch (Exception factoryE)
{
System.Web.HttpContext.Current.Trace.Warn("umbraco Utilities.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

Sep 11 '06 #1
5 4927
What is the exception ?

Is the default constructor visible ?

Regards,
Tasos

is**********@iassmarts.com wrote:
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.IUm bracoSearchFileFilter",
true);
//writeOutInterfaces(hasInterface);
if (hasInterface != null && !t.IsInterface)
{
//fails here
umbracoUtilities.Businesslogic.IUmbracoSearchFileF ilter typeInstance =
Activator.CreateInstance(t) as
umbracoUtilities.Businesslogic.IUmbracoSearchFileF ilter;

string extensions = ",";
if(typeInstance!=null)
{
foreach(string ext in typeInstance.extensions)
{
extensions += ext + ",";
_filters.Add(extensions,typeInstance);
System.Web.HttpContext.Current.Trace.Write("umbrac oUtilities.Search.FileFilterFactory",
" + Adding searchfilter for extensions '" + extensions + "'");
}
}
else{
System.Web.HttpContext.Current.Trace.Warn("umbraco Utilities.Search.FileFilterFactory",
" + Could not get instance for " +t.ToString() + " in '" + assembly +
"'");
}
}
}
}
catch (Exception factoryE)
{
System.Web.HttpContext.Current.Trace.Warn("umbraco Utilities.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
Sep 11 '06 #2
Tasos,

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

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

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

Sep 12 '06 #3
What is the exception ?
Ismail wrote:
Tasos,

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

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

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
Sep 12 '06 #4
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

Sep 12 '06 #5
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)

Ismail wrote:
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
Sep 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shmeian | last post by:
I have the following code which works fine. However I want to pass the object I'm instantiating a string for its constructor. I can't get the syntax right. Can someone give me an example of...
15
by: Brian Rogers | last post by:
Hello everyone, I apologize for the cross and re-post, but I am still searching for an answer. Why can C++ can create this object, but C# can't? I am trying to create an instance of the...
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
3
by: Doug Riley | last post by:
I am using CreateInstance to create an instance of a class and invoke a function of that class. I really need it to execute in a single line of code (long story, but I want to execute this code in...
3
by: System.Reflection Activator | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
1
by: hazz | last post by:
this is a repost with a hopefully more clearly stated scenario and more concise questions at the end. given the following two classes, my intent is to use pass a token into the instantiated class...
3
by: bahadirarslan | last post by:
Hi All, I have a problem with Activator.CreateInstance method. I have got a class like this public class YaziYaz { public YaziYaz() { HttpContext.Current.Response.Write("Running");
1
by: Johnny R | last post by:
Hello, I'm loading a Class from Assemly DLL using Activator.CreateInstance. That loaded Class is executed in a worker Thread with no loop. What actually happends when class is loaded using...
0
by: vignesh28 | last post by:
hi .. i got the following error in activator.createinstance method 'Exception has been thrown by the target of an invocation.' this is the error. here is my code: public object...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.