By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.