Type GetTypeFromNamePair (string name, string ns)
{
Type p = KnownTypeCollection.GetPrimitiveTypeFromName (name); // FIXME: namespace?
if (p != null)
return p;
bool makeArray = false;
if (name.StartsWith ("ArrayOf", StringComparison.Ordinal)) {
name = name.Substring (7); // strip "ArrayOf"
if (ns == KnownTypeCollection.MSArraysNamespace)
return GetTypeFromNamePair (name, String.Empty).MakeArrayType ();
makeArray = true;
}
string dnsb = KnownTypeCollection.DefaultClrNamespaceBase;
string clrns = ns.StartsWith (dnsb, StringComparison.Ordinal) ? ns.Substring (dnsb.Length) : ns;
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ()) {
Type [] types;
#if MOONLIGHT
try {
types = ass.GetTypes ();
} catch (System.Reflection.ReflectionTypeLoadException rtle) {
types = rtle.Types;
}
#else
types = ass.GetTypes ();
#endif
if (types == null)
continue;
foreach (var t in types) {
// there can be null entries or exception throw to access the attribute -
// at least when some referenced assemblies could not be loaded (affects moonlight)
if (t == null)
continue;
try {
var dca = t.GetCustomAttribute<DataContractAttribute> (true);
if (dca != null && dca.Name == name && dca.Namespace == ns)
return makeArray ? t.MakeArrayType () : t;
}
catch (TypeLoadException tle) {
Console.Error.WriteLine (tle);
continue;
}
catch (FileNotFoundException fnfe) {
Console.Error.WriteLine (fnfe);
continue;
}
if (clrns != null && t.Name == name && t.Namespace == clrns)
return makeArray ? t.MakeArrayType () : t;
}
}
throw new XmlException (String.Format ("Type not found; name: {0}, namespace: {1}", name, ns));
}
}