Ejemplo n.º 1
0
        static string GetAppBase(string appBase)
        {
            if (appBase == null)
            {
                return(null);
            }

            if (appBase.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
            {
                appBase = new Mono.Security.Uri(appBase).LocalPath;
                if (Path.DirectorySeparatorChar != '/')
                {
                    appBase = appBase.Replace('/', Path.DirectorySeparatorChar);
                }
            }
            appBase = Path.GetFullPath(appBase);

            if (Path.DirectorySeparatorChar != '/')
            {
                bool isExtendedPath = appBase.StartsWith(@"\\?\", StringComparison.Ordinal);
                if (appBase.IndexOf(':', isExtendedPath ? 6 : 2) != -1)
                {
                    throw new NotSupportedException("The given path's format is not supported.");
                }
            }

            // validate the path
            string dir = Path.GetDirectoryName(appBase);

            if ((dir != null) && (dir.LastIndexOfAny(Path.GetInvalidPathChars()) >= 0))
            {
                string msg = String.Format(Locale.GetText("Invalid path characters in path: '{0}'"), appBase);
                throw new ArgumentException(msg, "appBase");
            }

            string fname = Path.GetFileName(appBase);

            if ((fname != null) && (fname.LastIndexOfAny(Path.GetInvalidFileNameChars()) >= 0))
            {
                string msg = String.Format(Locale.GetText("Invalid filename characters in path: '{0}'"), appBase);
                throw new ArgumentException(msg, "appBase");
            }

            return(appBase);
        }
Ejemplo n.º 2
0
		public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
		{
			if (assemblyRef == null)
				throw new ArgumentNullException ("assemblyRef");

			if (assemblyRef.Name == null || assemblyRef.Name.Length == 0) {
				if (assemblyRef.CodeBase != null)
					return Assembly.LoadFrom (assemblyRef.CodeBase, assemblySecurity);
				else
					throw new ArgumentException (Locale.GetText ("assemblyRef.Name cannot be empty."), "assemblyRef");
			}

			Assembly assembly = LoadAssembly (assemblyRef.FullName, assemblySecurity, false);
			if (assembly != null)
				return assembly;

			if (assemblyRef.CodeBase == null)
				throw new FileNotFoundException (null, assemblyRef.Name);

			string cb = assemblyRef.CodeBase;
			if (cb.ToLower (CultureInfo.InvariantCulture).StartsWith ("file://"))
				cb = new Mono.Security.Uri (cb).LocalPath;

			try {
				assembly = Assembly.LoadFrom (cb, assemblySecurity);
			} catch {
				throw new FileNotFoundException (null, assemblyRef.Name);
			}
			AssemblyName aname = assembly.GetName ();
			// Name, version, culture, publickeytoken. Anything else?
			if (assemblyRef.Name != aname.Name)
				throw new FileNotFoundException (null, assemblyRef.Name);

			if (assemblyRef.Version != null && assemblyRef.Version != new Version (0, 0, 0, 0) && assemblyRef.Version != aname.Version)
				throw new FileNotFoundException (null, assemblyRef.Name);

			if (assemblyRef.CultureInfo != null && assemblyRef.CultureInfo.Equals (aname))
				throw new FileNotFoundException (null, assemblyRef.Name);

			byte [] pt = assemblyRef.GetPublicKeyToken ();
			if (pt != null && pt.Length != 0) {
				byte [] loaded_pt = aname.GetPublicKeyToken ();
				if (loaded_pt == null || (pt.Length != loaded_pt.Length))
					throw new FileNotFoundException (null, assemblyRef.Name);
				for (int i = pt.Length - 1; i >= 0; i--)
					if (loaded_pt [i] != pt [i])
						throw new FileNotFoundException (null, assemblyRef.Name);
			}
			return assembly;
		}