newbie: Assembly.Load

D

Dan

I have a strongly-named assembly in a DLL installed into the GAC. Let's say
that the DLL is named Test.Some.dll according to its project default
namespace, Test.Some. Of course the assembly has a public key and a version
number. I'd like to programmatically load this assembly or one of its types,
but the code cannot know in advance the version number for the assembly.
This is a problem because e.g.

Assembly.Load("Test.Some, Version=1.0.1548.27308, Culture=neutral,
PublicKeyToken=14954757e9893caf")

works fine but if I remove the Version it cannot work. I need not to bind my
code to a specific version of the required assembly, it should simply use
the latest available version in the GAC. Thus I should be able to find out
the (latest) version of the (GAC) assembly I need to load at runtime to load
that assembly. How can I do this?

Thx!
 
M

Mattias Sjögren

Dan,
it should simply use the latest available version in the GAC.

Back to DLL hell, eh?

If you don't care about versioning, why are you giving the assembly a
strong name?



Mattias
 
D

Dan

Yep, that's right, but I'm not trying to load the assembly to override the
strong-name mechanism; I was thinking at something like this to allow my
application to let the GAC dll deserialize an object serialized by a
previous version of itself, when the two versions can be defined as
compatible as for serialization only: while developing, whenever I rebuild
the DLL I get a (de)serialization exception when loading a previosly
serialized object.

I was trying to use a solution to the versioning problem posted about 1 year
ago in a newsgroup, which relies on a SerializationBinder-derived class plus
a custom attribute applied to the DLL classes which need serialization. This
attribute specifies a version number which tells the binder that for a
specified namespace any request for a class greater than or equal to the
specified version can be satisfied by this version. I don't know if there is
a better solution to this problem, but this one appealed to me because it
mantains the version checking mechanism while allowing to reuse serialized
objects until their contents can be considered compatible.

Here it is what the Binder method does: have a look at the TODO comment:

public override Type BindToType(string sAssemblyName, string sTypeName)
{
Type typeToDeserialize = null;

try
{
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
sTypeName, sAssemblyName));
} //etry
catch (Exception){}

if ((typeToDeserialize == null) && (sTypeName.StartsWith(_sPrefix)))
{
Match m = _rVer.Match(sAssemblyName);

if (m.Success)
{
Version verFile = new Version(m.Groups[1].Value), verMin;
string sNonVer = _rVer.Replace(sAssemblyName,"");
Type tNew = Type.GetType(String.Format("{0}, {1}", sTypeName,
sNonVer));

// TODO here tNew CAN BE NULL because the assembly
// is a strong-typed assembly which requires a version number
// to be loaded. In such case instead of just failing I should get the
highest version
// number from the assemblies in the GAC and try using its assembly.

if (tNew == null) throw new SerializationException(
String.Format("Cannot find the object {0}, {1}", sTypeName,sNonVer));

object[] atrs =
tNew.GetCustomAttributes(typeof(SerializationCompatibilityAttribute),false);
if ((atrs != null) && (atrs.Length > 0))
{
if (atrs.Length > 1)
throw new SerializationException
("Multiple SerializationCompatibility attributes");
verMin = ((SerializationCompatibilityAttribute)
atrs[0]).MinimumVersion;
} //eif
else
{
verMin = tNew.Assembly.GetName().Version;
verMin = new Version(verMin.Major,verMin.Minor,0,0);
} //eelse

if (verFile >= verMin) return tNew;
} //eif
} //eif
return typeToDeserialize;
}
 
M

Mattias Sjögren

I was thinking at something like this to allow my
application to let the GAC dll deserialize an object serialized by a
previous version of itself, when the two versions can be defined as
compatible as for serialization only:

Wouldn't setting the formatter's AssemblyFormat property to
FormatterAssemblyStyle.Simple allow that too?

while developing, whenever I rebuild
the DLL I get a (de)serialization exception when loading a previosly
serialized object.

Perhaps you should consider not changing the assembly version for
every build. See

http://blogs.msdn.com/suzcook/archive/2003/05/29/57148.aspx



Mattias
 
J

Jeffrey Tan[MSFT]

Hi Dan,

Does the Mattias's reply make sense to you?

Please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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