Esempio n. 1
0
        public static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assemblyName = new AssemblyName(args.Name).Name + ".dll";
            if(assemblyName.Contains(".resources"))
            {
                //Console.WriteLine("AssemblyResolve: {0}", assemblyName);
                return null;
            }

            string fullAssemblyName = string.Empty;
            foreach(var manifestResource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
            {
                if(manifestResource.Contains(assemblyName))
                {
                    fullAssemblyName = manifestResource;
                    break;
                }
            }

            if(fullAssemblyName == string.Empty)
                return null;

            using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fullAssemblyName))
            {
                byte[] assemblyData = new byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        }
Esempio n. 2
0
        public void Run()
        {
            string absoluteAssemblyDir = Path.GetFullPath(BuildAssemblyPath);
            string assemblyPath = Path.Combine(absoluteAssemblyDir, BuildAssemblyName);

            AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
            {
                // FIXME: Imprecise
                var requestedAssemblyName = new AssemblyName(e.Name).Name;
                if (! requestedAssemblyName.Contains("System"))
                {
                    string requestedAssemblyDll = requestedAssemblyName + ".dll";

                    foreach (string reference in References)
                    {
                        if (reference.ToLowerInvariant().Contains(requestedAssemblyDll.ToLowerInvariant()))
                            return Assembly.LoadFrom(reference);
                    }

                    var candidateAssemblyPath = Path.Combine(absoluteAssemblyDir, requestedAssemblyDll);
                    if (File.Exists(candidateAssemblyPath))
                        return Assembly.LoadFrom(candidateAssemblyPath);
                }

                return null;
            };

            string serializersAssemblyPath = Path.ChangeExtension(assemblyPath, ".XmlSerializers.dll");
            Assembly assembly = Assembly.LoadFrom(assemblyPath);

            List<Type> serializableTypes = new List<Type>();
            foreach (Type type in assembly.GetExportedTypes())
            {
                if (type.IsDefined(typeof(XmlRootAttribute), false))
                    serializableTypes.Add(type);
            }

            List<XmlMapping> mappings = new List<XmlMapping>();
            XmlReflectionImporter importer = new XmlReflectionImporter();

            foreach (Type type in serializableTypes)
            {
                XmlMapping mapping = importer.ImportTypeMapping(type);
                mappings.Add(mapping);
            }

            CompilerParameters compilerParameters = new CompilerParameters();

            compilerParameters.TempFiles = new TempFileCollection();
            compilerParameters.IncludeDebugInformation = false;
            compilerParameters.GenerateInMemory = false;
            compilerParameters.OutputAssembly = serializersAssemblyPath;

            if (KeyFile != null)
                compilerParameters.CompilerOptions += " /keyfile:\"" + KeyFile + "\"";

            XmlSerializer.GenerateSerializer(serializableTypes.ToArray(),
                mappings.ToArray(), compilerParameters);
        }
        /// <summary>
        /// Event handler method to resolve DLL assemblies that can't be found automatically.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        internal static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            Assembly assembly;
            string keyName = new AssemblyName(args.Name).Name;
            if (keyName.Contains(".resources"))
            {
                return null;  // This line is what fixed the problem
            }

            //Go through hint paths to see if we can help add assembly references
            var referenceFromHintPaths = _hintPaths.Where(p => p.EndsWith(string.Format(@"\{0}.dll", keyName)));
            foreach (var hintPath in referenceFromHintPaths)
            {
                assembly = Assembly.LoadFile(hintPath);
                if (!_libs.ContainsKey(keyName))
                {
                    _libs.Add(keyName, assembly);
                }
            }

            if (_libs.ContainsKey(keyName))
            {
                assembly = _libs[keyName]; // If DLL is loaded then don't load it again just return
                return assembly;
            }

            string dllName = DllResourceName(keyName);
            //string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();   // Uncomment this line to debug the possible values for dllName
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(dllName))
            {
                if (stream == null)
                {
                    LoggerBase.Log("Error! Unable to find '" + dllName + "'", true);
                    // Uncomment the next lines to show message the moment an assembly is not found. (This will also stop for .Net assemblies
                    //MessageBox.Show("Error! Unable to find '" + dllName + "'! Application will terminate.");
                    //Environment.Exit(0);
                    return null;
                }

                byte[] buffer = new BinaryReader(stream).ReadBytes((int)stream.Length);
                assembly = Assembly.Load(buffer);

                _libs[keyName] = assembly;
                return assembly;
            }
        }
Esempio n. 4
0
 Type FindType(string name)
 {
     foreach (string use in usings)
     {
         name = (use != "" ? use + "." : "") + name;
         if (name.Contains('.'))
         {
             name += ".";
             int i = 0;
             i = name.IndexOf('.');
             string first = name.Substring(0, i);
             Type t = null;
             while ((t = Type.GetType(name.Substring(0, i))) == null)
             {
                 i = name.IndexOf('.', i + 1);
             }
             return t;
         }
         else
         {
             Type t = Type.GetType(name);
             if (t != null) return t;
         }
     }
     throw new Exception("Type not found: " + name);
 }
Esempio n. 5
0
 /// <summary>
 /// Resolve local path on HDInsight VM where the ADF .Net activity is running
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
 {
     var assemblyname = new AssemblyName(args.Name).Name;
     if (assemblyname.Contains("Microsoft.WindowsAzure.Storage"))
     {
         assemblyname = "Microsoft.WindowsAzure.Storage";
         var assemblyFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), assemblyname + ".dll");
         var assembly = Assembly.LoadFrom(assemblyFileName);
         return assembly;
     }
     return null;
 }