Komak57

ProxyDomain.cs

Sep 6th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace DataStripper
  10. {
  11.     [SerializableAttribute]
  12.     [ComVisibleAttribute(true)]
  13.     class ProxyDomain : MarshalByRefObject
  14.     {
  15.         private object _instance;
  16.         public object Instance
  17.         {
  18.             get { return _instance; }
  19.         }
  20.         /*private AppDomain _domain;
  21.         public AppDomain Domain
  22.         {
  23.             get
  24.             {
  25.                 return _domain;
  26.             }
  27.  
  28.         }*/
  29.         public AppDomain CreateDomain(string friendlyName, System.Security.Policy.Evidence securityinfo)
  30.         {
  31.             return AppDomain.CreateDomain(friendlyName, securityinfo);
  32.         }
  33.         public void UnloadDomain(AppDomain _domain)
  34.         {
  35.             try
  36.             {
  37.                 AppDomain.Unload(_domain);
  38.             }
  39.             catch (ArgumentNullException dne)
  40.             {
  41.                 // ignore null exceptions
  42.                 return;
  43.             }
  44.         }
  45.         private Assembly _assembly;
  46.         public Assembly Assembly
  47.         {
  48.             get
  49.             {
  50.                 return _assembly;
  51.             }
  52.         }
  53.         private byte[] loadFile(string filename)
  54.         {
  55.             FileStream fs = new FileStream(filename, FileMode.Open);
  56.             byte[] buffer = new byte[(int)fs.Length];
  57.             fs.Read(buffer, 0, buffer.Length);
  58.             fs.Close();
  59.  
  60.             return buffer;
  61.         }
  62.         public void LoadAssembly(AppDomain _domain, string path, string typeName)
  63.         {
  64.             try
  65.             {
  66.                 if (_domain == null)
  67.                     throw new ArgumentNullException("_domain does not exist.");
  68.                 byte[] Assembly_data = loadFile(path);
  69.                 byte[] Symbol_data = loadFile(path.Replace(".dll", ".pdb"));
  70.  
  71.                 //_assembly = _domain.Load(Assembly_data, Symbol_data);
  72.                 _assembly = _domain.Load(AssemblyName.GetAssemblyName(path));
  73.                 _type = _assembly.GetType(typeName);
  74.  
  75.                 _instance = (object)_domain.CreateInstanceAndUnwrap(
  76.                     _assembly.FullName,
  77.                     typeName);
  78.             }
  79.             catch (Exception ex)
  80.             {
  81.                 throw new InvalidOperationException(ex.ToString());
  82.             }
  83.         }
  84.         private Type _type;
  85.         public Type Type
  86.         {
  87.             get
  88.             {
  89.                 return _type;
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment