-
ASP.NET 프로젝트에서 런타임에 참조된 어셈블리 불러오기ASP.NET 2010. 3. 31. 23:38
ASP.NET 프로젝트에서는 Visual Studio에서 참조된 어셈블리 외에도 bin 폴더에 포함된 모든 어셈블리를 참조된 어셈블리로 인식합니다.
외부에서 컴파일 된 어셈블리 또한 런타임에 bin폴더로 추가하여도 정상적으로 동작하게 됩니다.그렇다면, ASP.NET 프로젝트에서 참조된 모든 어셈블리의 목록을 어떻게 가져올 수 있을까요?
이는 System.Web.Compliation.BuildManager 클래스의 GetReferencedAssemblies 정적 메서드를 이용하여 알 수 있습니다.만일 참조된 어셈블리에 존재하는 WCF RIA Services의 DomainService의 하위 클래스의 목록을 가져오려 한다면 아래와 같은 코드를 이용할 수 있습니다.
Dictionary<string, Type> dictionary = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
Type type = typeof(DomainService);
foreach (Assembly assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>())
{
Type[] exportedTypes = null;
try
{
exportedTypes = assembly.GetExportedTypes();
}
catch (ReflectionTypeLoadException exception)
{
exportedTypes = exception.Types;
}
catch (Exception)
{
}
if (exportedTypes != null)
{
foreach (Type type2 in exportedTypes)
{
if ((!type2.IsAbstract && !type2.IsInterface) &&
(!type2.IsValueType && type.IsAssignableFrom(type2)) &&
(TypeDescriptor.GetAttributes(type2)[typeof(EnableClientAccessAttribute)] != null))
{
string canonicalFileName = GetCanonicalFileName(type2);
dictionary[canonicalFileName] = type2;
}
}
}
}
위 코드에서는 BuildManager.GetReferencedAssemblies 메서드를 통하여 참조로 등록된 어셈블리들을 가져오고,
Type.IsAssignableFrom(Type) 메서드를 이용하여 현재 타입이 DomainService를 상속받고 있는지를 확인합니다.
그리고, TypeDescriptor.GetAttributes 메서드를 이용하여 등록된 특성 중에 EnableClientAccess 가 있는지를 확인하게 됩니다.위와 같은 방식으로 BuildManager 클래스와 리플렉션을 이용하여 런타임에 참조된 어셈블리를 불러와서 다양한 활용이 가능해집니다.
'ASP.NET' 카테고리의 다른 글
ASP.NET 보안 취약성 업데이트 공개 (0) 2010.09.29 ASP.NET MVC 에서 사용자 정의 컨트롤의 내용을 문자열로 받기 (0) 2010.01.14 ASP.NET Generated Image 컨트롤 (0) 2008.09.05 Visual Studio 2008 한글판에서 ASP.NET MVC 프로젝트 생성 (0) 2008.08.23 Response.TransmitFile을 이용한 다운로드 (0) 2007.06.04