Why Reflection? UnnDunn Sep 5th, 2013 1,646 Never 1,646Never

Not a member of Pastebin yet? Sign Up , it unlocks many cool features!

rawdownloadcloneembedreportprint C# 2.22 KB using System ; using System.Collections.Generic ; using System.Linq ; using System.Web ; using System.Reflection ; namespace TmpLabs . Mobile . PublicSite . Core . Mvc { public static class MimeExtensionHelper { static object locker = new object ( ) ; static object mimeMapping ; static MethodInfo getMimeMappingMethodInfo ; static MimeExtensionHelper ( ) { Type mimeMappingType = Assembly . GetAssembly ( typeof ( HttpRuntime ) ) . GetType ( "System.Web.MimeMapping" ) ; if ( mimeMappingType == null ) { throw new SystemException ( "Couldnt find MimeMapping type" ) ; } ConstructorInfo constructorInfo = mimeMappingType . GetConstructor ( BindingFlags . NonPublic | BindingFlags . Instance , null , Type . EmptyTypes , null ) ; if ( constructorInfo == null ) { throw new SystemException ( "Couldnt find default constructor for MimeMapping" ) ; } mimeMapping = constructorInfo . Invoke ( null ) ; if ( mimeMapping == null ) { throw new SystemException ( "Couldnt find MimeMapping" ) ; } getMimeMappingMethodInfo = mimeMappingType . GetMethod ( "GetMimeMapping" , BindingFlags . Static | BindingFlags . NonPublic ) ; if ( getMimeMappingMethodInfo == null ) { throw new SystemException ( "Couldnt find GetMimeMapping method" ) ; } if ( getMimeMappingMethodInfo . ReturnType != typeof ( String ) ) { throw new SystemException ( "GetMimeMapping method has invalid return type" ) ; } if ( getMimeMappingMethodInfo . GetParameters ( ) . Length != 1 && getMimeMappingMethodInfo . GetParameters ( ) [ 0 ] . ParameterType != typeof ( String ) ) { throw new SystemException ( "GetMimeMapping method has invalid parameters" ) ; } } public static String GetMimeType ( string filename ) { lock ( locker ) { return ( String ) getMimeMappingMethodInfo . Invoke ( mimeMapping, new object [ ] { filename } ) ; } } } }

RAW Paste Data

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; namespace TmpLabs.Mobile.PublicSite.Core.Mvc { public static class MimeExtensionHelper { static object locker = new object(); static object mimeMapping; static MethodInfo getMimeMappingMethodInfo; static MimeExtensionHelper() { Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping"); if (mimeMappingType == null) { throw new SystemException("Couldnt find MimeMapping type"); } ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (constructorInfo == null) { throw new SystemException("Couldnt find default constructor for MimeMapping"); } mimeMapping = constructorInfo.Invoke(null); if (mimeMapping == null) { throw new SystemException("Couldnt find MimeMapping"); } getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic); if (getMimeMappingMethodInfo == null) { throw new SystemException("Couldnt find GetMimeMapping method"); } if (getMimeMappingMethodInfo.ReturnType != typeof(String)) { throw new SystemException("GetMimeMapping method has invalid return type"); } if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(String)) { throw new SystemException("GetMimeMapping method has invalid parameters"); } } public static String GetMimeType(string filename) { lock (locker) { return (String)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename }); } } } }