コード例 #1
0
        public static bool CreateFileFromResource(string name, string filePath, string dllPath = null)
        {
            Assembly dll = dllPath == null?Assembly.GetCallingAssembly() : Assembly.LoadFrom(dllPath);

            string[] names = dll.GetManifestResourceNames();
            if (!names.Contains(name))
            {
                throw new ArgumentException(string.Format("{0}, expect: {1}", name, string.Join(";", names)));
            }
            var stream = dll.GetManifestResourceStream(name);
            var file   = new FileInfo(filePath);

            if (file.Exists)
            {
                Guid checksum = CryptoManaged.MD5Hash(stream);
                stream.Position = 0L;
                using (var fileStream = file.OpenRead())
                {
                    if (checksum == CryptoManaged.MD5Hash(fileStream))
                    {
                        return(false);
                    }
                }
            }
            using (var fileStream = file.OpenWrite())
            {
                stream.FixedCopyTo(fileStream);
            }
            return(true);
        }
コード例 #2
0
        public Assembly Inject(Guid checksum, object arg, Stream rawStream = null)
        {
            Contract.Requires(checksum != Guid.Empty);

            if (rawStream != null)
            {
                var raw = new MemoryStream();
                rawStream.FixedCopyTo(raw);
                raw.Position = 0L;
                Guid checksumNew = CryptoManaged.MD5Hash(raw);
                if (checksum != checksumNew)
                {
                    throw new InvalidOperationException("checksum");
                }
                return(this.Mapper.GetOrAdd(checksum, k => _Domain.Load(raw.ToArray())));
            }
            Assembly assembly;

            if (!this.Mapper.TryGetValue(checksum, out assembly))
            {
                throw new InvalidOperationException("checksum");
            }
            var eType = typeof(IAppEntry);
            var q     = from t in assembly.GetTypes()
                        where t.IsSubclassOf(eType)
                        select t;
            var entryType = q.FirstOrDefault();

            if (entryType != null)
            {
                var creator = entryType.GetConstructor(Type.EmptyTypes);
                if (creator != null)
                {
                    var entry = (IAppEntry)creator.Invoke(null);
                    entry.DoEntry(arg);
                }
            }
            return(assembly);
        }