Type.GetType not finding the dll containing the method I want to use in a late bound context.

H

hazz

"Value cannot be null.\r\nParameter name: type" is the exception thrown
after the CreateInstance method below.

Type t = Type.GetType(GetConfigValue("PasswordProvider"));
IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t);

GetConfigValue DOES return the correct value from the config file -
"namespace.DBPassword, namespace"

It appears that Type.GetType is NOT finding the assembly whose name is the
same as the namespace above (DBPassword is the name of the class contained
within that assembly/dll.

In the remarks section for Type.GetType I read the following;

If the assembly has not been saved to disk when GetType is called, the
method returns a null reference . GetType does not understand transient
dynamic assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns a null reference (Nothing).

I can see that the dll DOES exist in the /bin/debug directory with a recent
timestamp. I would assume it has already been saved to disk when the search
is made for it.

Any ideas on how to troubleshoot this?

Thank you,
Greg
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

You should load the assembly first, through a call to the Load method on
the Assembly class (make sure to use the full assembly name). Fusion
*should* pick it up if it is in the bin directory.

Once you have that, you can call CreateInstance on the Assembly instance
returned, passing the full type name.

Hope this helps.
 
H

hazz

Thank you Nicholas. I couldn't figure out the syntax for Load on the first
pass and used LoadFrom.
LoadFrom uses a hard coded path to the assembly however. That has to go. I
got the proof of concept working with the late-bouund functionality but that
will have to be massaged.
The following is a mess. I will have to figure out why my previous code
which looked up the asssembly name in the app.config (or app.exe.config)
file is not working.

MethodInfo mi;

Assembly ass = Assembly.LoadFrom("O:\\A.B.C\\bin\\Debug\\A.B.C.dll");

Type[] types = ass.GetTypes();

foreach (Type t in types)

{

mi = t.GetMethod("GetPassword");

if (mi != null)

{

object[]args = new object[]{token};

object obj = Activator.CreateInstance(t,null);

string pwd =
(string)t.InvokeMember("GetPassword",BindingFlags.InvokeMethod,null,obj,args
);

}

greg

Nicholas Paldino said:
Greg,

You should load the assembly first, through a call to the Load method on
the Assembly class (make sure to use the full assembly name). Fusion
*should* pick it up if it is in the bin directory.

Once you have that, you can call CreateInstance on the Assembly instance
returned, passing the full type name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
"Value cannot be null.\r\nParameter name: type" is the exception thrown
after the CreateInstance method below.

Type t = Type.GetType(GetConfigValue("PasswordProvider"));
IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t);

GetConfigValue DOES return the correct value from the config file -
"namespace.DBPassword, namespace"

It appears that Type.GetType is NOT finding the assembly whose name is the
same as the namespace above (DBPassword is the name of the class contained
within that assembly/dll.

In the remarks section for Type.GetType I read the following;

If the assembly has not been saved to disk when GetType is called, the
method returns a null reference . GetType does not understand transient
dynamic assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns a null reference (Nothing).

I can see that the dll DOES exist in the /bin/debug directory with a recent
timestamp. I would assume it has already been saved to disk when the search
is made for it.

Any ideas on how to troubleshoot this?

Thank you,
Greg
 
H

hazz

If Load requires the full name which includes version number, I would like
to avoid its use as well. Having to know the version number seems to be as
significant a constraint as a hard coded path.
-Greg

Nicholas Paldino said:
Greg,

You should load the assembly first, through a call to the Load method on
the Assembly class (make sure to use the full assembly name). Fusion
*should* pick it up if it is in the bin directory.

Once you have that, you can call CreateInstance on the Assembly instance
returned, passing the full type name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
"Value cannot be null.\r\nParameter name: type" is the exception thrown
after the CreateInstance method below.

Type t = Type.GetType(GetConfigValue("PasswordProvider"));
IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t);

GetConfigValue DOES return the correct value from the config file -
"namespace.DBPassword, namespace"

It appears that Type.GetType is NOT finding the assembly whose name is the
same as the namespace above (DBPassword is the name of the class contained
within that assembly/dll.

In the remarks section for Type.GetType I read the following;

If the assembly has not been saved to disk when GetType is called, the
method returns a null reference . GetType does not understand transient
dynamic assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns a null reference (Nothing).

I can see that the dll DOES exist in the /bin/debug directory with a recent
timestamp. I would assume it has already been saved to disk when the search
is made for it.

Any ideas on how to troubleshoot this?

Thank you,
Greg
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

If you can't know the version number, then you will have to know the
path. The best way to do this is to define a well-known loacation (relative
to your assembly, perhaps), where the other assemblies need to be placed.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
If Load requires the full name which includes version number, I would like
to avoid its use as well. Having to know the version number seems to be as
significant a constraint as a hard coded path.
-Greg

message news:%[email protected]...
Greg,

You should load the assembly first, through a call to the Load
method
on
the Assembly class (make sure to use the full assembly name). Fusion
*should* pick it up if it is in the bin directory.

Once you have that, you can call CreateInstance on the Assembly instance
returned, passing the full type name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
"Value cannot be null.\r\nParameter name: type" is the exception thrown
after the CreateInstance method below.

Type t = Type.GetType(GetConfigValue("PasswordProvider"));
IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t);

GetConfigValue DOES return the correct value from the config file -
"namespace.DBPassword, namespace"

It appears that Type.GetType is NOT finding the assembly whose name is the
same as the namespace above (DBPassword is the name of the class contained
within that assembly/dll.

In the remarks section for Type.GetType I read the following;

If the assembly has not been saved to disk when GetType is called, the
method returns a null reference . GetType does not understand transient
dynamic assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns a null reference (Nothing).

I can see that the dll DOES exist in the /bin/debug directory with a recent
timestamp. I would assume it has already been saved to disk when the search
is made for it.

Any ideas on how to troubleshoot this?

Thank you,
Greg
 
H

hazz

Nicholas,

I got it! Now I can use the app.config value to change the location of the
assembly if need be.

<appSettings>
<add key="assembly_Get_DB_Password" value="A.B.Get_DB_Password.DBPassword,
A.B.Get_DB_Password"/>

The code looks to the config file for the name of the assembly in the app's
output directory where the executable and app.exe.config file exist.

Type t = Type.GetType(GetValueFromFile("assembly_Get_DB_Password"));
IGet_DB_Password o_latebound =
(IGet_DB_Password)Activator.CreateInstance(t);

string strPassword = o_latebound .GetPassword(token); // where
GetPassword is a method in the assembly that has been dynamically loaded.

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

public static string GetValueFromFile(string key)

string filename =
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

if(File.Exists(filename))

{

val=ConfigurationSettings.AppSettings[key]; //where key is
the PasswordProvider above that got passed in here..

return val;

}

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

Thanks for your suggestions Nicholas.

-Greg Hazzard

Nicholas Paldino said:
Greg,

If you can't know the version number, then you will have to know the
path. The best way to do this is to define a well-known loacation (relative
to your assembly, perhaps), where the other assemblies need to be placed.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hazz said:
If Load requires the full name which includes version number, I would like
to avoid its use as well. Having to know the version number seems to be as
significant a constraint as a hard coded path.
-Greg

message news:%[email protected]...
Greg,

You should load the assembly first, through a call to the Load
method
on
the Assembly class (make sure to use the full assembly name). Fusion
*should* pick it up if it is in the bin directory.

Once you have that, you can call CreateInstance on the Assembly instance
returned, passing the full type name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"Value cannot be null.\r\nParameter name: type" is the exception thrown
after the CreateInstance method below.

Type t = Type.GetType(GetConfigValue("PasswordProvider"));
IPasswordProvider ppdr= (IPasswordProvider)Activator.CreateInstance(t);

GetConfigValue DOES return the correct value from the config file -
"namespace.DBPassword, namespace"

It appears that Type.GetType is NOT finding the assembly whose name
is
the
same as the namespace above (DBPassword is the name of the class contained
within that assembly/dll.

In the remarks section for Type.GetType I read the following;

If the assembly has not been saved to disk when GetType is called, the
method returns a null reference . GetType does not understand transient
dynamic assemblies; therefore, calling GetType to retrieve a type in a
transient dynamic assembly returns a null reference (Nothing).

I can see that the dll DOES exist in the /bin/debug directory with a
recent
timestamp. I would assume it has already been saved to disk when the
search
is made for it.

Any ideas on how to troubleshoot this?

Thank you,
Greg
 

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