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

  • Thread starter Thread starter hazz
  • Start date Start date
H

hazz

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\\Debug\\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.CreateInstance(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
 
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));
 
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 said:
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 said:
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\\Debug\\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.CreateInstance(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
 
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,
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 said:
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);
 
Back
Top