■ 관리 DLL 여부를 구하는 방법을 보여준다.
[TestLibrary 프로젝트]
▶ ICLR4Support.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
namespace TestLibrary { /// <summary> /// CLR 4.0 지원 인터페이스 /// </summary> public interface ICLR4Support { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 관리 여부 구하기 - IsManaged(filePath) /// <summary> /// 관리 여부 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>관리 여부</returns> bool IsManaged(string filePath); #endregion } } |
▶ DLLHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
using Microsoft.CSharp; using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; namespace TestLibrary { /// <summary> /// DLL 헬퍼 /// </summary> public class DLLHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// CLR 4.0 지원 객체 /// </summary> private static ICLR4Support _clr4Support; /// <summary> /// 동기 객체 /// </summary> private static object _syncObject = new object(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region CLR 4.0 지원 객체 - CLR4Support /// <summary> /// CLR 4.0 지원 객체 /// </summary> private static ICLR4Support CLR4Support { get { if(_clr4Support == null) { lock(_syncObject) { if(_clr4Support == null) { Dictionary<string, string> optionDictionary = new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }; CSharpCodeProvider provider = new CSharpCodeProvider(optionDictionary); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add(typeof(DLLHelper).Assembly.Location); string classText = "using System.Reflection;" + Environment.NewLine + "using TestLibrary;" + Environment.NewLine + "class CLR4Support : ICLR4Support" + Environment.NewLine + "{" + Environment.NewLine + " public bool IsManaged(string filePath)" + Environment.NewLine + " {" + Environment.NewLine + " try" + Environment.NewLine + " {" + Environment.NewLine + " return Assembly.ReflectionOnlyLoadFrom(filePath) != null;" + Environment.NewLine + " }" + Environment.NewLine + " catch" + Environment.NewLine + " {" + Environment.NewLine + " return false;" + Environment.NewLine + " }" + Environment.NewLine + " }" + Environment.NewLine + "}"; CompilerResults results = null; try { results = provider.CompileAssemblyFromSource(parameters, classText); } catch { return null; } if(results.Errors.Count > 0) { Console.WriteLine("Errors building into {0}", results.PathToAssembly); foreach(CompilerError error in results.Errors) { Console.WriteLine(" {0}", error.ToString()); Console.WriteLine(); } } else { Type clr4SupportType = results.CompiledAssembly.GetType("CLR4Support"); ConstructorInfo constructorInfo = clr4SupportType.GetConstructor(Type.EmptyTypes); _clr4Support = constructorInfo.Invoke(new object[] { }) as ICLR4Support; } } } } return _clr4Support; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 관리 여부 구하기 - IsManaged(filePath) /// <summary> /// 관리 여부 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>관리 여부</returns> public static bool IsManaged(string filePath) { try { return Assembly.ReflectionOnlyLoadFrom(filePath) != null; } catch(Exception exception) { int resultHandle = Marshal.GetHRForException(exception); if(resultHandle == unchecked((int)0x8013101b)) { if(CLR4Support != null) { return CLR4Support.IsManaged(filePath); } } return false; } } #endregion } } |
[TestProject 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.IO; using TestLibrary; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Title = "관리 DLL 여부 구하기"; string workingDirectoryPath = Path.GetDirectoryName(typeof(Program).Assembly.Location); Console.WriteLine("닷넷 4.0 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "net40.dll" ))); Console.WriteLine("닷넷 2.0 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "net20.dll" ))); Console.WriteLine("네이티브 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "native.dll"))); } #endregion } } |