473,323 Members | 1,622 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,323 software developers and data experts.

"Constructor not found" exception using Activator.CreateInstance. Is InvokeMember a better choice?

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 a token into the instantiated
class DBPassword and return a string;
**************************************

namespace DBPasswordProvider
public class DBPassword
{
public DBPassword()
{
//Empty constructor
}
public string GetPassword(Token token)
{

string sPassword = "Howdy Doody";
return sPassword;
}
}

******************what I will pass into DB.GetPassword.GetPassword above
********************
public class Token
{
protected string m_strPWD;
public Token()
{
}
public string Password
{
get
{
return m_strPWD;
}

set
{
m_strPWD=value;
}
}

**************************************

Token tkn = new Token();

tkn.Password = "password";

object[] args = {tkn}; //This is my intention, how do I do this?

Assembly assmbly =
Assembly.LoadFrom("O:\\DBPasswordProvider\\bin\\De bug\\DBPasswordProvider.dl
l");

Type[] types = assmbly.GetTypes();
foreach (Type t in types)
{
mi = t.GetMethod("GetPassword");

if (mi != null)
{
string typeName = t.FullName;

{
DBPasswordProvider.DBPassword ppdr =
(DBPasswordProvider.DBPassword)Activator.CreateIns tance(t,BindingFlags.Publi
c | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args,
null);
**************************************

When I run the above, "Constructor on type DBPasswordProvider.DBPassword not
found." } System.Exception is thrown.

Why would this exception be thrown? There is a constructor for public
DBPassword.

Should I use Activator.CreateInstance or InvokeMember?

How do I pass in the token as an arg if I do use CreateInstance?

Which of the 9 overloaded versions of Activator.CreateInstance would I
choose for this?
Thank you very much,

-Greg


Nov 16 '05 #1
7 11947
You're passing an array of arguments to the constructor, but the only
constructor accepts no parameters. You must first create the instance of
DBPassword, then invoke the GetPassword method. Try this:

string typeName = t.FullName;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null);
Console.WriteLine(ppdr.GetPassword(tkn));

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

"hazz" <ha**@sonic.net> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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 a token into the instantiated class DBPassword and return a string;
**************************************

namespace DBPasswordProvider
public class DBPassword
{
public DBPassword()
{
//Empty constructor
}
public string GetPassword(Token token)
{

string sPassword = "Howdy Doody";
return sPassword;
}
}

******************what I will pass into DB.GetPassword.GetPassword above
********************
public class Token
{
protected string m_strPWD;
public Token()
{
}
public string Password
{
get
{
return m_strPWD;
}

set
{
m_strPWD=value;
}
}

**************************************

Token tkn = new Token();

tkn.Password = "password";

object[] args = {tkn}; //This is my intention, how do I do this?

Assembly assmbly =
Assembly.LoadFrom("O:\\DBPasswordProvider\\bin\\De bug\\DBPasswordProvider.dl l");

Type[] types = assmbly.GetTypes();
foreach (Type t in types)
{
mi = t.GetMethod("GetPassword");

if (mi != null)
{
string typeName = t.FullName;

{
DBPasswordProvider.DBPassword ppdr =
(DBPasswordProvider.DBPassword)Activator.CreateIns tance(t,BindingFlags.Publi c | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args,
null);
**************************************

When I run the above, "Constructor on type DBPasswordProvider.DBPassword not found." } System.Exception is thrown.

Why would this exception be thrown? There is a constructor for public
DBPassword.

Should I use Activator.CreateInstance or InvokeMember?

How do I pass in the token as an arg if I do use CreateInstance?

Which of the 9 overloaded versions of Activator.CreateInstance would I
choose for this?
Thank you very much,

-Greg

Nov 16 '05 #2
Thank you so much Mickey. Progress!! Activator.CreateInstance sounds so
formidable and scary that I couldn't even see that I was not instantiating
the class before accessing the method....... O.K. I just plain didn't get
it... :-(
The line;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null)
throws a "specified cast is not valid" exception.
Let me see if I can figure that out.
Appreciatively,
-Greg Hazzard

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You're passing an array of arguments to the constructor, but the only
constructor accepts no parameters. You must first create the instance of
DBPassword, then invoke the GetPassword method. Try this:

string typeName = t.FullName;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null);
Console.WriteLine(ppdr.GetPassword(tkn));

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

"hazz" <ha**@sonic.net> wrote in message
news:eU**************@TK2MSFTNGP10.phx.gbl...
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 a token into the

instantiated
class DBPassword and return a string;
**************************************

namespace DBPasswordProvider
public class DBPassword
{
public DBPassword()
{
//Empty constructor
}
public string GetPassword(Token token)
{

string sPassword = "Howdy Doody";
return sPassword;
}
}

******************what I will pass into DB.GetPassword.GetPassword above
********************
public class Token
{
protected string m_strPWD;
public Token()
{
}
public string Password
{
get
{
return m_strPWD;
}

set
{
m_strPWD=value;
}
}

**************************************

Token tkn = new Token();

tkn.Password = "password";

object[] args = {tkn}; //This is my intention, how do I do this?

Assembly assmbly =

Assembly.LoadFrom("O:\\DBPasswordProvider\\bin\\De bug\\DBPasswordProvider.dl
l");

Type[] types = assmbly.GetTypes();
foreach (Type t in types)
{
mi = t.GetMethod("GetPassword");

if (mi != null)
{
string typeName = t.FullName;

{
DBPasswordProvider.DBPassword ppdr =

(DBPasswordProvider.DBPassword)Activator.CreateIns tance(t,BindingFlags.Publi
c | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args,
null);
**************************************

When I run the above, "Constructor on type DBPasswordProvider.DBPassword

not
found." } System.Exception is thrown.

Why would this exception be thrown? There is a constructor for public
DBPassword.

Should I use Activator.CreateInstance or InvokeMember?

How do I pass in the token as an arg if I do use CreateInstance?

Which of the 9 overloaded versions of Activator.CreateInstance would I
choose for this?
Thank you very much,

-Greg


Nov 16 '05 #3
"hazz" <ha**@sonic.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
The line;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null)
throws a "specified cast is not valid" exception.


If you both dynamically *and* explictly bind to an assembly, you'll get
that. It's a long story. Try this code to stay dynamic:

object[] args = new object[]{tkn};
object obj = Activator.CreateInstance(t, null);
string pwd = (string)t.InvokeMember("GetPassword",
BindingFlags.InvokeMethod, null, obj, args);

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
Nov 16 '05 #4
Mickey,
Thank you, thank you, thank you.
For those of us who have to sometimes understand the general concept by
reasoning up from particular working examples, your help as been invaluable.
Through virtual serendipity, your real time wisdom and by your simply taking
the time to help, IT WORKED!
If there would be anyway I could help you to even audit one of Jean-Raymond
Abrial's class after a 1 month Goethe intensive language brush-up course, I
would do it!
I can at least support you in your vision.....
Appreciatively,
Greg Hazzard

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"hazz" <ha**@sonic.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
The line;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null)
throws a "specified cast is not valid" exception.


If you both dynamically *and* explictly bind to an assembly, you'll get
that. It's a long story. Try this code to stay dynamic:

object[] args = new object[]{tkn};
object obj = Activator.CreateInstance(t, null);
string pwd = (string)t.InvokeMember("GetPassword",
BindingFlags.InvokeMethod, null, obj, args);

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

Nov 16 '05 #5
Mickey,
Thank you, thank you, thank you.
For those of us who have to sometimes understand the general concept by
reasoning up from particular working examples, your help as been invaluable.
Through virtual serendipity, your real time wisdom and by your simply taking
the time to help, IT WORKED!
If there would be anyway I could help you to even audit one of Jean-Raymond
Abrial's class after a 1 month Goethe intensive language brush-up course, I
would do it!
I can at least support you in your vision.....
Appreciatively,
Greg Hazzard

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"hazz" <ha**@sonic.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
The line;
DBPassword ppdr = (DBPassword)Activator.CreateInstance(t, null)
throws a "specified cast is not valid" exception.


If you both dynamically *and* explictly bind to an assembly, you'll get
that. It's a long story. Try this code to stay dynamic:

object[] args = new object[]{tkn};
object obj = Activator.CreateInstance(t, null);
string pwd = (string)t.InvokeMember("GetPassword",
BindingFlags.InvokeMethod, null, obj, args);

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey

Nov 16 '05 #6
"hazz" <ha**@sonic.net> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
Mickey,
Thank you, thank you, thank you.


You're welcome -- glad that helped.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
Nov 16 '05 #7
"hazz" <ha**@sonic.net> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl...
Mickey,
Thank you, thank you, thank you.


You're welcome -- glad that helped.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
Nov 16 '05 #8

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

Similar topics

3
by: hazz | last post by:
Is Activator.CreateInstance(t,BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args, null) appropriate given the following; ...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
0
by: ttomic | last post by:
I have asp.net application written in jscript. On POST request, during Page_Load, I receive string which I evaluate using jscript eval method. String looks like: my_fun("arg") where my_fun is...
4
by: Dan Stromberg | last post by:
Hi folks. I'm working on building some software, some of which is written in C++, for a researcher here at the University. I have an extensive background in C and python, but I haven't done...
8
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a...
3
by: Charles Law | last post by:
This is driving me mad. Can someone please put me out of my misery? I have a DataSet which I wish to use as the data source for a grid control (it is actually an Infragistics grid). When I assign...
0
by: active | last post by:
Sometimes when I After I click "Start Debugging" I get a "File not Found" Exception. Could not load file or assembly 'FormTesting, Version=1.0.2646.36738, Culture=neutral, PublicKeyToken=null'...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
5
by: Sendil kumar | last post by:
Hi All, The FtpWebRequest.GetResponse( ) method is not giving "550 File not found exception " when I do a directory list operation on a invalid(not present) directory in HP Unix and Sun Solaris...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.