System.Type & QAN.... Will 'version' break things down the line?

G

Guest

Yesterday I was told that GetType(string) should not just be with a Type, but
be Type, AssemblyName. Fair enough, get the reason. (Finally!).

As long as it doesn't cause tech support problems down the line...
What happens when my code is run on a station that only has framework 3.0 or
4.0, and this assembly, with version number defined for 2.0.0.0 , isn't
available. ...breaks?

Second question:
Does an assembly's PublicKeyToken change for every release of the framework?
Or is it defined for good? I'm asking this because I've found that some Types
can be gotten with simply the assemblyname, and sometimes nothing works
except for a FullyQualifiedAssemblyName.

Hence my question as to what is the cost down the road to specifying Version
and PublicKeyToken information.

And then there is just the don't get it part, still:
Frankly, I don't understand why some Types it can find with just a partial
AssemblyName, no version or anything...and others, like this Type below,
needs absolutely everything before it comes back non-null. Can I get it to be
a little less strict?

//STUDY:

//Get a real instance and see what its FQAN is:
System.Type to = typeof(System.Configuration.ConfigurationManager);

//This is what I get back:
string AssemblyQualifiedName = type.AssemblyQualifiedName;
//which was:
//"System.Configuration.ConfigurationManager, System.Configuration,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"


//Here we try to get the type via reflection with variations of partial
names...
//With terrible results
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=\"\"", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=\"en\"", false, true));

resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral, StrongName =null", false ,true));

//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=null,Culture=neutral", false ,true));
//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=\"\",Culture=neutral", false ,true));

resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2", false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0", false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=null", false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=\"\"", false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=neutral", false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.*", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral, Version=2.0.0.0", false, true));
//Error:
//resultList.Add(System.Type.GetTyp
("System.Configuration.ConfigurationManager,
PublicKeyToken=b03f5f7f11d50a3a", false, true));
//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=null,Culture=neutral, PublicKeyToken=\"\"",
false ,true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, PublicKeyToken=b03f5f7f11d50a3a", false, true))
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=neutral", false, true));

//Only one that works:
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a",false ,true));

//If I breakpoint here to check, the only non-null is the last one...
System.Type configurationManagerType = resultList[resultList.Count-1];
 
G

Guest

Aha!!!


After trying 20 or so variations...only one I hadn't tried was:


resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a",false ,true));

Guess what? It works.

but this doesn't:


resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a",false ,true));


Conclusion:
Although at first it looks really scary to use an Assembly name tied to a
version that may not exist in the future (such as binding to 2.0, and later
-- many eons from now -- when the code runs on a new OS that has NET 4.0 on
it, but not 2.0) it will not break!
Why?
a) The name will be the same if in the NET framework
b) The PublicKeyToken will be the same for all versions of it (I think,
atleast it seems reasonable, if it was snk'd before being released the first
time)
c) The culture is generally going to be neutral in most cases
d) The version though, will try to find the exact match if it can -- if it
can't, it will increment till it finds the first and nearest version.

Ok. I can relax now. This finally puts to rest my worrying about using FQN
in config files, when I was trying to do everything to avoid them, and
running into serious trouble loading from GAC with only partial names. No
need to. Bind to a FQN of the version you currently have -- it won't break in
future (except for 'breaking changes' the Framework, but there we are talking
about method signatures, not assembly names).

Night,
Sky






Sky said:
Yesterday I was told that GetType(string) should not just be with a Type, but
be Type, AssemblyName. Fair enough, get the reason. (Finally!).

As long as it doesn't cause tech support problems down the line...
What happens when my code is run on a station that only has framework 3.0 or
4.0, and this assembly, with version number defined for 2.0.0.0 , isn't
available. ...breaks?

Second question:
Does an assembly's PublicKeyToken change for every release of the framework?
Or is it defined for good? I'm asking this because I've found that some Types
can be gotten with simply the assemblyname, and sometimes nothing works
except for a FullyQualifiedAssemblyName.

Hence my question as to what is the cost down the road to specifying Version
and PublicKeyToken information.

And then there is just the don't get it part, still:
Frankly, I don't understand why some Types it can find with just a partial
AssemblyName, no version or anything...and others, like this Type below,
needs absolutely everything before it comes back non-null. Can I get it to be
a little less strict?

//STUDY:

//Get a real instance and see what its FQAN is:
System.Type to = typeof(System.Configuration.ConfigurationManager);

//This is what I get back:
string AssemblyQualifiedName = type.AssemblyQualifiedName;
//which was:
//"System.Configuration.ConfigurationManager, System.Configuration,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"


//Here we try to get the type via reflection with variations of partial
names...
//With terrible results:
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=\"\"", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=\"en\"", false, true));

resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral, StrongName =null", false ,true));

//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=null,Culture=neutral", false ,true));
//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=\"\",Culture=neutral", false ,true));

resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2", false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0", false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=null", false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=\"\"", false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=neutral", false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.*", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Culture=neutral, Version=2.0.0.0", false, true));
//Error:
//resultList.Add(System.Type.GetTyp
("System.Configuration.ConfigurationManager,
PublicKeyToken=b03f5f7f11d50a3a", false, true));
//Error:
//resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=null,Culture=neutral, PublicKeyToken=\"\"",
false ,true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, PublicKeyToken=b03f5f7f11d50a3a", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0", false, true));
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager, System.Configuration, Version=2.0.0.0, Culture=neutral", false, true));

//Only one that works:
resultList.Add(System.Type.GetType("System.Configuration.ConfigurationManager,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a",false ,true));

//If I breakpoint here to check, the only non-null is the last one...
System.Type configurationManagerType = resultList[resultList.Count-1];
 

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