Esempio n. 1
0
        /// <summary>
        /// Tries load a type into current domain.
        /// </summary>
        /// <remarks>This method can cross AppDomain boundary.</remarks>
        /// <param name="assemblyQualifiedTypeName">An assembly qualified name of the type.</param>
        /// <returns>A tpair of 'success' and 'result'. The result is a full assembly qualified type name, or an error message in case success==false.</returns>
        public Tuple <bool, string> TryLoadType(string assemblyQualifiedTypeName)
        {
            if (null != isolatedInstance)
            {
                return(isolatedInstance.TryLoadType(assemblyQualifiedTypeName));
            }

            try
            {
                var match = typeNameRegEx.Match(assemblyQualifiedTypeName);
                if (match.Success) //throw new FormatException("Wrong format of assembly qualified type name: " + assemblyQualifiedTypeName);
                {
                    var typeName     = match.Groups["type"].Value;
                    var assemblyName = match.Groups["assembly"].Value;
                    var aname        = new AssemblyName(assemblyName);
                    var asm          = AppDomain.CurrentDomain.Load(aname);
                    var t            = asm.GetType(typeName);
                    if (null == t)
                    {
                        return(Tuple.Create(false, string.Format("The type {0} was not found in {1}.", typeName, asm.FullName)));
                    }
                    return(Tuple.Create(true, t.AssemblyQualifiedName));
                }
                // try interpret the argument as an assembly name and find a proper type in it
                try
                {
                    var aname    = new AssemblyName(assemblyQualifiedTypeName);
                    var asm      = AppDomain.CurrentDomain.Load(aname);
                    var handlers = asm.GetTypes()
                                   .Where(t => typeof(DataSourceHandler).IsAssignableFrom(t))
                                   .ToList();
                    if (0 == handlers.Count)
                    {
                        return(Tuple.Create(false, "No data handler found in " + asm.FullName));
                    }
                    if (1 < handlers.Count)
                    {
                        return(Tuple.Create(false, "No data handler found in " + asm.FullName));
                    }
                    return(Tuple.Create(true, handlers[0].AssemblyQualifiedName));
                }
                catch (FormatException)
                {
                    return(Tuple.Create(false, "The argument is not a type name nor an assembly name: " + assemblyQualifiedTypeName));
                }
            }
            catch (Exception e)
            {
                return(Tuple.Create(false, e.Message));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// extracts the assembly and type for the data hanfler from supplied string which can be either a type name or a dll name
 /// </summary>
 /// <param name="handler">a type name or a dll name</param>
 /// <param name="toLoad"></param>
 /// <param name="handlerType"></param>
 private static string ExtractHandlerAssemblyAndTypeName(string handler, AssemblyStore gac)
 {
     if (handler.EndsWith("dll", true, CultureInfo.InvariantCulture))
     {
         try
         {
             var toLoad = System.Reflection.Assembly.LoadFrom(handler);
             var types  = toLoad.GetExportedTypes().Where(t => t.IsSubclassOf(typeof(Microsoft.Research.Science.FetchClimate2.DataSourceHandler))).ToArray();
             if (types.Length == 0)
             {
                 throw new Exception("Specifed dll doesn't contain classes inheried from Microsoft.Research.Science.FetchClimate2.DataSourceHandler");
             }
             else if (types.Length > 1)
             {
                 throw new Exception("Specifed dll contains more than one class specification inheried from Microsoft.Research.Science.FetchClimate2.DataSourceHandler. You can specify FullTypeName of the handler instead");
             }
             else
             {
                 return(types[0].AssemblyQualifiedName);
             }
         }
         catch (Exception ex)
         {
             throw new Exception("Failed to load data handler from " + handler + "\n Exception message: " + ex.Message);
         }
     }
     else
     {
         if (gac != null)
         {
             var result = gac.TryLoadType(handler);
             if (result.Item1)
             {
                 return(result.Item2);
             }
             else
             {
                 throw new Exception(result.Item2);
             }
         }
         else
         {
             return(Type.GetType(handler).AssemblyQualifiedName);
         }
     }
 }